File: type.go

package info (click to toggle)
golang-github-vektah-gqlparser 2.5.12-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,748 kB
  • sloc: javascript: 164; sh: 46; makefile: 10
file content (68 lines) | stat: -rw-r--r-- 1,210 bytes parent folder | download
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 (t *Type) Dump() string {
	return t.String()
}