Skip to content

Commit

Permalink
clean up naming, unnecessary interface usage
Browse files Browse the repository at this point in the history
  • Loading branch information
benweint committed May 30, 2024
1 parent 8bc8035 commit 3b361f1
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 51 deletions.
2 changes: 1 addition & 1 deletion pkg/commands/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 ""
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/graph/edge.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
10 changes: 3 additions & 7 deletions pkg/graph/field_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
}
Expand Down
6 changes: 2 additions & 4 deletions pkg/graph/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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:
Expand Down
20 changes: 8 additions & 12 deletions pkg/model/arguments.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ import (
)

// Based on the __InputValue introspection type.
type InputValueDefinition struct {
type ArgumentDefinition struct {
Name string
Description string
DefaultValue Value
Type *Type
Directives DirectiveList
}

type InputValueDefinitionList []*InputValueDefinition
type ArgumentDefinitionList []*ArgumentDefinition

type ArgumentList []*Argument

Expand All @@ -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,
Expand All @@ -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
}
Expand All @@ -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
Expand All @@ -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),
Expand Down
12 changes: 6 additions & 6 deletions pkg/model/directives.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
18 changes: 7 additions & 11 deletions pkg/model/fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
}
Expand Down
12 changes: 4 additions & 8 deletions pkg/model/name_reference.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,18 @@ const (
)

type NameReference struct {
Kind NameReferenceKind
typeRef *Definition
field *FieldDefinition
inputField *InputValueDefinition
Kind NameReferenceKind
typeRef *Definition
field *FieldDefinition
}

func (n *NameReference) GetTargetType() *Definition {
return n.typeRef
}

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 ""
}

0 comments on commit 3b361f1

Please sign in to comment.