aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/vektah/gqlparser/ast/type.go
blob: 9577fdb48cbc73827f3e96e0367afbbf5f4ea1ac (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package ast

func NonNullNamedType(named string, pos *Position) *Type {
	return &Type{NamedType: named, NonNull: true, Position: pos}
}

func NamedType(named string, pos *Position) *Type {
	return &Type{NamedType: named, NonNull: false, Position: pos}
}

func NonNullListType(elem *Type, pos *Position) *Type {
	return &Type{Elem: elem, NonNull: true, Position: pos}
}

func ListType(elem *Type, pos *Position) *Type {
	return &Type{Elem: elem, NonNull: false, Position: pos}
}

type Type struct {
	NamedType string
	Elem      *Type
	NonNull   bool
	Position  *Position `dump:"-"`
}

func (t *Type) Name() string {
	if t.NamedType != "" {
		return t.NamedType
	}

	return t.Elem.Name()
}

func (t *Type) String() string {
	nn := ""
	if t.NonNull {
		nn = "!"
	}
	if t.NamedType != "" {
		return t.NamedType + nn
	}

	return "[" + t.Elem.String() + "]" + nn
}

func (t *Type) IsCompatible(other *Type) bool {
	if t.NamedType != other.NamedType {
		return false
	}

	if t.Elem != nil && other.Elem == nil {
		return false
	}

	if t.Elem != nil && !t.Elem.IsCompatible(other.Elem) {
		return false
	}

	if other.NonNull {
		return t.NonNull
	}

	return true
}

func (v *Type) Dump() string {
	return v.String()
}