• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

99designs / gqlgen / 12320669056

13 Dec 2024 06:01PM UTC coverage: 73.884% (-0.04%) from 73.919%
12320669056

push

github

web-flow
enable testifylint.encoded-compare and fix lint issues (#3434)

8575 of 11606 relevant lines covered (73.88%)

641.17 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

64.81
/codegen/object.go
1
package codegen
2

3
import (
4
        "fmt"
5
        "go/types"
6
        "strconv"
7
        "strings"
8
        "unicode"
9

10
        "github.com/vektah/gqlparser/v2/ast"
11
        "golang.org/x/text/cases"
12
        "golang.org/x/text/language"
13

14
        "github.com/99designs/gqlgen/codegen/config"
15
)
16

17
type GoFieldType int
18

19
const (
20
        GoFieldUndefined GoFieldType = iota
21
        GoFieldMethod
22
        GoFieldVariable
23
        GoFieldMap
24
)
25

26
type Object struct {
27
        *ast.Definition
28

29
        Type                     types.Type
30
        ResolverInterface        types.Type
31
        Root                     bool
32
        Fields                   []*Field
33
        Implements               []*ast.Definition
34
        DisableConcurrency       bool
35
        Stream                   bool
36
        Directives               []*Directive
37
        PointersInUnmarshalInput bool
38
}
39

40
func (b *builder) buildObject(typ *ast.Definition) (*Object, error) {
125✔
41
        dirs, err := b.getDirectives(typ.Directives)
125✔
42
        if err != nil {
125✔
43
                return nil, fmt.Errorf("%s: %w", typ.Name, err)
×
44
        }
×
45
        caser := cases.Title(language.English, cases.NoLower)
125✔
46
        obj := &Object{
125✔
47
                Definition:               typ,
125✔
48
                Root:                     b.Config.IsRoot(typ),
125✔
49
                DisableConcurrency:       typ == b.Schema.Mutation,
125✔
50
                Stream:                   typ == b.Schema.Subscription,
125✔
51
                Directives:               dirs,
125✔
52
                PointersInUnmarshalInput: b.Config.ReturnPointersInUnmarshalInput,
125✔
53
                ResolverInterface: types.NewNamed(
125✔
54
                        types.NewTypeName(0, b.Config.Exec.Pkg(), caser.String(typ.Name)+"Resolver", nil),
125✔
55
                        nil,
125✔
56
                        nil,
125✔
57
                ),
125✔
58
        }
125✔
59

125✔
60
        if !obj.Root {
235✔
61
                goObject, err := b.Binder.DefaultUserObject(typ.Name)
110✔
62
                if err != nil {
111✔
63
                        return nil, err
1✔
64
                }
1✔
65
                obj.Type = goObject
109✔
66
        }
67

68
        for _, intf := range b.Schema.GetImplements(typ) {
134✔
69
                obj.Implements = append(obj.Implements, b.Schema.Types[intf.Name])
10✔
70
        }
10✔
71

72
        for _, field := range typ.Fields {
676✔
73
                if strings.HasPrefix(field.Name, "__") {
576✔
74
                        continue
24✔
75
                }
76

77
                var f *Field
528✔
78
                f, err = b.buildField(obj, field)
528✔
79
                if err != nil {
528✔
80
                        return nil, err
×
81
                }
×
82

83
                obj.Fields = append(obj.Fields, f)
528✔
84
        }
85

86
        return obj, nil
124✔
87
}
88

89
func (o *Object) Reference() types.Type {
304✔
90
        if config.IsNilable(o.Type) {
304✔
91
                return o.Type
×
92
        }
×
93
        return types.NewPointer(o.Type)
304✔
94
}
95

96
type Objects []*Object
97

98
func (o *Object) Implementors() string {
62✔
99
        satisfiedBy := strconv.Quote(o.Name)
62✔
100
        for _, s := range o.Implements {
62✔
101
                satisfiedBy += ", " + strconv.Quote(s.Name)
×
102
        }
×
103
        return "[]string{" + satisfiedBy + "}"
62✔
104
}
105

106
func (o *Object) HasResolvers() bool {
218✔
107
        for _, f := range o.Fields {
1,120✔
108
                if f.IsResolver {
944✔
109
                        return true
42✔
110
                }
42✔
111
        }
112
        return false
176✔
113
}
114

115
func (o *Object) HasUnmarshal() bool {
12✔
116
        if o.IsMap() {
12✔
117
                return false
×
118
        }
×
119
        for i := 0; i < o.Type.(*types.Named).NumMethods(); i++ {
12✔
120
                if o.Type.(*types.Named).Method(i).Name() == "UnmarshalGQL" {
×
121
                        return true
×
122
                }
×
123
        }
124
        return false
12✔
125
}
126

127
func (o *Object) HasDirectives() bool {
×
128
        if len(o.Directives) > 0 {
×
129
                return true
×
130
        }
×
131
        for _, f := range o.Fields {
×
132
                if f.HasDirectives() {
×
133
                        return true
×
134
                }
×
135
        }
136

137
        return false
×
138
}
139

140
func (o *Object) IsConcurrent() bool {
146✔
141
        for _, f := range o.Fields {
808✔
142
                if f.IsConcurrent() {
670✔
143
                        return true
8✔
144
                }
8✔
145
        }
146
        return false
138✔
147
}
148

149
func (o *Object) IsReserved() bool {
124✔
150
        return strings.HasPrefix(o.Definition.Name, "__")
124✔
151
}
124✔
152

153
func (o *Object) IsMap() bool {
30✔
154
        return o.Type == config.MapType
30✔
155
}
30✔
156

157
func (o *Object) Description() string {
×
158
        return o.Definition.Description
×
159
}
×
160

161
func (o *Object) HasField(name string) bool {
×
162
        for _, f := range o.Fields {
×
163
                if f.Name == name {
×
164
                        return true
×
165
                }
×
166
        }
167

168
        return false
×
169
}
170

171
func (os Objects) ByName(name string) *Object {
66✔
172
        for i, o := range os {
555✔
173
                if strings.EqualFold(o.Definition.Name, name) {
539✔
174
                        return os[i]
50✔
175
                }
50✔
176
        }
177
        return nil
16✔
178
}
179

180
func ucFirst(s string) string {
×
181
        if s == "" {
×
182
                return ""
×
183
        }
×
184

185
        r := []rune(s)
×
186
        r[0] = unicode.ToUpper(r[0])
×
187
        return string(r)
×
188
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc