Skip to content

Commit

Permalink
rename config to "StructReferences" & rework check
Browse files Browse the repository at this point in the history
The logic check was subtly flawed and would override
any omitempty: false comments
  • Loading branch information
nathanstitt committed Nov 11, 2021
1 parent 19726c7 commit 77da957
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 24 deletions.
10 changes: 5 additions & 5 deletions docs/genqlient.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,14 @@ context_type: context.Context
client_getter: "github.com/you/yourpkg.GetClient"


# if set, fields with a complex type will default to having
# the "pointer: true, omitempty: true" flag
# if set, fields with a struct type will default to having
# the "pointer: true, omitempty: true" flag.
#
# This can be useful for complex schema where it would be burdensome
# to manually set the flags on a large number of fields
# This can be useful for struct schema where it would be burdensome
# to manually set the flags on a large number of fields.
#
# Defaults to false.
use_weak_references: boolean
use_struct_references: boolean


# A map from GraphQL type name to Go fully-qualified type name to override
Expand Down
2 changes: 1 addition & 1 deletion generate/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type Config struct {
ContextType string `yaml:"context_type"`
ClientGetter string `yaml:"client_getter"`
Bindings map[string]*TypeBinding `yaml:"bindings"`
WeakReferences bool `yaml:"use_weak_references"`
StructReferences bool `yaml:"use_struct_references"`

// Set to true to use features that aren't fully ready to use.
//
Expand Down
28 changes: 13 additions & 15 deletions generate/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ func (g *generator) convertArguments(
// names.go) and the selection-set (we use all the input type's fields,
// and so on recursively). See also the `case ast.InputObject` in
// convertDefinition, below.
goTyp, err := g.convertType(nil, arg.Type, false, nil, options, queryOptions)
goTyp, err := g.convertType(nil, arg.Type, nil, options, queryOptions)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -218,7 +218,6 @@ func (g *generator) convertArguments(
func (g *generator) convertType(
namePrefix *prefixList,
typ *ast.Type,
isStructField bool,
selectionSet ast.SelectionSet,
options, queryOptions *genqlientDirective,
) (goType, error) {
Expand All @@ -236,7 +235,7 @@ func (g *generator) convertType(
if typ.Elem != nil {
// Type is a list.
elem, err := g.convertType(
namePrefix, typ.Elem, isStructField, selectionSet, options, queryOptions)
namePrefix, typ.Elem, selectionSet, options, queryOptions)
return &goSliceType{elem}, err
}

Expand All @@ -246,12 +245,14 @@ func (g *generator) convertType(
goTyp, err := g.convertDefinition(
namePrefix, def, typ.Position, selectionSet, options, queryOptions)

if g.getWeakReference(options, isStructField, def) {
if options.Omitempty == nil {
if g.getStructReference(options, def) {
if options.Pointer == nil || *options.Pointer == true {
goTyp = &goPointerType{goTyp}
}
if options.Omitempty == nil || *options.Omitempty == true {
oe := true
options.Omitempty = &oe
}
goTyp = &goPointerType{goTyp}
} else if options.GetPointer() {
// Whatever we get, wrap it in a pointer. (Because of the way the
// options work, recursing here isn't as connvenient.)
Expand All @@ -261,16 +262,13 @@ func (g *generator) convertType(
return goTyp, err
}

// getWeakReference decides if a field should be of pointer type and have the omitempty flag set.
func (g *generator) getWeakReference(
// getStructReference decides if a field should be of pointer type and have the omitempty flag set.
func (g *generator) getStructReference(
options *genqlientDirective,
isStructField bool,
def *ast.Definition,
) bool {
return options.Pointer == nil &&
isStructField &&
g.Config.WeakReferences &&
len(def.Fields) > 0
return g.Config.StructReferences &&
(def.Kind == ast.Object || def.Kind == ast.InputObject)
}

// convertDefinition decides the Go type we will generate corresponding to a
Expand Down Expand Up @@ -424,7 +422,7 @@ func (g *generator) convertDefinition(
// will be ignored? We know field.Type is a scalar, enum, or input
// type. But plumbing that is a bit tricky in practice.
fieldGoType, err := g.convertType(
namePrefix, field.Type, true, nil, fieldOptions, queryOptions)
namePrefix, field.Type, nil, fieldOptions, queryOptions)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -847,7 +845,7 @@ func (g *generator) convertField(
namePrefix = nextPrefix(namePrefix, field)

fieldGoType, err := g.convertType(
namePrefix, field.Definition.Type, true, field.SelectionSet,
namePrefix, field.Definition.Type, field.SelectionSet,
fieldOptions, queryOptions)
if err != nil {
return nil, err
Expand Down
6 changes: 3 additions & 3 deletions generate/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,9 @@ func TestGenerateWithConfig(t *testing.T) {
Generated: "generated.go",
ContextType: "github.com/Khan/genqlient/internal/testutil.MyContext",
}},
{"WeakReferences", "", &Config{
WeakReferences: true,
Generated: "generated-weakrefs.go",
{"StructReferences", "", &Config{
StructReferences: true,
Generated: "generated-structrefs.go",
}},
{"NoContext", "", &Config{
Generated: "generated.go",
Expand Down

0 comments on commit 77da957

Please sign in to comment.