From 3b361f1f6f171eb75de399cbb6098dd87c7fba9c Mon Sep 17 00:00:00 2001 From: Ben Weintraub Date: Wed, 29 May 2024 23:51:25 -0700 Subject: [PATCH] clean up naming, unnecessary interface usage --- pkg/commands/helpers.go | 2 +- pkg/graph/edge.go | 3 +-- pkg/graph/field_filter.go | 10 +++------- pkg/graph/graph.go | 6 ++---- pkg/model/arguments.go | 20 ++++++++------------ pkg/model/directives.go | 12 ++++++------ pkg/model/fields.go | 18 +++++++----------- pkg/model/name_reference.go | 12 ++++-------- 8 files changed, 32 insertions(+), 51 deletions(-) diff --git a/pkg/commands/helpers.go b/pkg/commands/helpers.go index b777977..d7fe167 100644 --- a/pkg/commands/helpers.go +++ b/pkg/commands/helpers.go @@ -64,7 +64,7 @@ func parseSchemaFromPaths(paths []string) (*ast.Schema, error) { return schema, nil } -func formatArgumentDefinitionList(al model.InputValueDefinitionList) string { +func formatArgumentDefinitionList(al model.ArgumentDefinitionList) string { if len(al) == 0 { return "" } diff --git a/pkg/graph/edge.go b/pkg/graph/edge.go index f51ced8..50efda1 100644 --- a/pkg/graph/edge.go +++ b/pkg/graph/edge.go @@ -16,7 +16,6 @@ type edge struct { dst *node kind edgeKind field *model.FieldDefinition - inputField *model.InputValueDefinition - argument *model.InputValueDefinition + argument *model.ArgumentDefinition possibleType string } diff --git a/pkg/graph/field_filter.go b/pkg/graph/field_filter.go index 0636fea..a64e4ac 100644 --- a/pkg/graph/field_filter.go +++ b/pkg/graph/field_filter.go @@ -65,10 +65,6 @@ type fieldFilter struct { includeFields map[string]bool } -type fieldLike interface { - FieldName() string -} - func (f *fieldFilter) merge(other *fieldFilter) { if other.includeAll { f.includeAll = true @@ -80,14 +76,14 @@ func (f *fieldFilter) merge(other *fieldFilter) { } } -func applyFilter[T fieldLike](f *fieldFilter, list []T) []T { +func applyFilter(f *fieldFilter, list model.FieldDefinitionList) model.FieldDefinitionList { if f.includeAll { return list } - var result []T + var result model.FieldDefinitionList for _, field := range list { - if f.includeFields[field.FieldName()] { + if f.includeFields[field.Name] { result = append(result, field) } } diff --git a/pkg/graph/graph.go b/pkg/graph/graph.go index ef7ef37..47ce3ba 100644 --- a/pkg/graph/graph.go +++ b/pkg/graph/graph.go @@ -133,7 +133,7 @@ func (g *Graph) makeUnionEdges(t *model.Definition) []*edge { return result } -func (g *Graph) makeFieldEdge(src *model.Definition, targetType *model.Type, f *model.FieldDefinition, arg *model.InputValueDefinition) *edge { +func (g *Graph) makeFieldEdge(src *model.Definition, targetType *model.Type, f *model.FieldDefinition, arg *model.ArgumentDefinition) *edge { kind := edgeKindField if arg != nil { @@ -265,10 +265,8 @@ func (g *Graph) buildEdgeDefs() []string { } switch edge.kind { - case edgeKindField: + case edgeKindField, edgeKindInputField: srcPortSuffix = ":" + portName(edge.field.Name) - case edgeKindInputField: - srcPortSuffix = ":" + portName(edge.inputField.Name) case edgeKindArgument: srcPortSuffix = ":" + portNameForArgument(edge.field.Name, edge.argument.Name) case edgeKindPossibleType: diff --git a/pkg/model/arguments.go b/pkg/model/arguments.go index 4b74ec6..feb9631 100644 --- a/pkg/model/arguments.go +++ b/pkg/model/arguments.go @@ -7,7 +7,7 @@ import ( ) // Based on the __InputValue introspection type. -type InputValueDefinition struct { +type ArgumentDefinition struct { Name string Description string DefaultValue Value @@ -15,7 +15,7 @@ type InputValueDefinition struct { Directives DirectiveList } -type InputValueDefinitionList []*InputValueDefinition +type ArgumentDefinitionList []*ArgumentDefinition type ArgumentList []*Argument @@ -24,11 +24,7 @@ type Argument struct { Value Value `json:"value"` } -func (a *InputValueDefinition) FieldName() string { - return a.Name -} - -func (a *InputValueDefinition) MarshalJSON() ([]byte, error) { +func (a *ArgumentDefinition) MarshalJSON() ([]byte, error) { m := map[string]any{ "name": a.Name, "type": a.Type, @@ -51,10 +47,10 @@ func (a *InputValueDefinition) MarshalJSON() ([]byte, error) { return json.Marshal(m) } -func makeInputValueDefinitionListFromArgs(in ast.ArgumentDefinitionList) (InputValueDefinitionList, error) { - var result InputValueDefinitionList +func makeArgumentDefinitionList(in ast.ArgumentDefinitionList) (ArgumentDefinitionList, error) { + var result ArgumentDefinitionList for _, a := range in { - argDef, err := makeInputValueDefinition(a.Name, a.Description, a.Type, a.Directives, a.DefaultValue) + argDef, err := makeArgumentDefinition(a.Name, a.Description, a.Type, a.Directives, a.DefaultValue) if err != nil { return nil, err } @@ -63,7 +59,7 @@ func makeInputValueDefinitionListFromArgs(in ast.ArgumentDefinitionList) (InputV return result, nil } -func makeInputValueDefinition(name, description string, inType *ast.Type, inDirectives ast.DirectiveList, inDefaultValue *ast.Value) (*InputValueDefinition, error) { +func makeArgumentDefinition(name, description string, inType *ast.Type, inDirectives ast.DirectiveList, inDefaultValue *ast.Value) (*ArgumentDefinition, error) { defaultValue, err := makeValue(inDefaultValue) if err != nil { return nil, err @@ -72,7 +68,7 @@ func makeInputValueDefinition(name, description string, inType *ast.Type, inDire if err != nil { return nil, err } - return &InputValueDefinition{ + return &ArgumentDefinition{ Name: name, Description: description, Type: makeType(inType), diff --git a/pkg/model/directives.go b/pkg/model/directives.go index 741036b..3ffdee1 100644 --- a/pkg/model/directives.go +++ b/pkg/model/directives.go @@ -17,18 +17,18 @@ type DirectiveList []*Directive // DirectiveDefinition represents the definition of a directive. // Based on the __Directive introspection type defined here: https://spec.graphql.org/October2021/#sec-The-__Directive-Type type DirectiveDefinition struct { - Description string `json:"description"` - Name string `json:"name"` - Arguments InputValueDefinitionList `json:"arguments,omitempty"` - Locations []ast.DirectiveLocation `json:"locations"` - IsRepeatable bool `json:"repeatable"` + Description string `json:"description"` + Name string `json:"name"` + Arguments ArgumentDefinitionList `json:"arguments,omitempty"` + Locations []ast.DirectiveLocation `json:"locations"` + IsRepeatable bool `json:"repeatable"` } // DirectiveDefinitionList represents a list of directive definitions. type DirectiveDefinitionList []*DirectiveDefinition func makeDirectiveDefinition(in *ast.DirectiveDefinition) (*DirectiveDefinition, error) { - args, err := makeInputValueDefinitionListFromArgs(in.Arguments) + args, err := makeArgumentDefinitionList(in.Arguments) if err != nil { return nil, err } diff --git a/pkg/model/fields.go b/pkg/model/fields.go index 3916864..eed0521 100644 --- a/pkg/model/fields.go +++ b/pkg/model/fields.go @@ -16,21 +16,17 @@ import ( // - As a result of the above, the deprecated and deprecationReason fields are omitted, since they would // duplicate the content of the more generic directives field. type FieldDefinition struct { - Name string `json:"name"` - Description string `json:"description,omitempty"` - Type *Type `json:"type"` - Arguments InputValueDefinitionList `json:"arguments,omitempty"` // only for fields - DefaultValue Value `json:"defaultValue,omitempty"` // only for input values - Directives DirectiveList `json:"directives,omitempty"` + Name string `json:"name"` + Description string `json:"description,omitempty"` + Type *Type `json:"type"` + Arguments ArgumentDefinitionList `json:"arguments,omitempty"` // only for fields + DefaultValue Value `json:"defaultValue,omitempty"` // only for input values + Directives DirectiveList `json:"directives,omitempty"` } // FieldDefinitionList represents a set of fields definitions on the same object, interface, or input type. type FieldDefinitionList []*FieldDefinition -func (fd *FieldDefinition) FieldName() string { - return fd.Name -} - func (fd *FieldDefinition) MarshalJSON() ([]byte, error) { m := map[string]any{ "name": fd.Name, @@ -67,7 +63,7 @@ func makeFieldDefinitionList(in ast.FieldList) (FieldDefinitionList, error) { return nil, err } - args, err := makeInputValueDefinitionListFromArgs(f.Arguments) + args, err := makeArgumentDefinitionList(f.Arguments) if err != nil { return nil, err } diff --git a/pkg/model/name_reference.go b/pkg/model/name_reference.go index e6f6adb..ca80614 100644 --- a/pkg/model/name_reference.go +++ b/pkg/model/name_reference.go @@ -9,10 +9,9 @@ const ( ) type NameReference struct { - Kind NameReferenceKind - typeRef *Definition - field *FieldDefinition - inputField *InputValueDefinition + Kind NameReferenceKind + typeRef *Definition + field *FieldDefinition } func (n *NameReference) GetTargetType() *Definition { @@ -20,11 +19,8 @@ func (n *NameReference) GetTargetType() *Definition { } func (n *NameReference) GetFieldName() string { - switch n.Kind { - case FieldNameReference: + if n.field != nil { return n.field.Name - case InputFieldNameReference: - return n.inputField.Name } return "" }