Skip to content

Commit

Permalink
refactor: use fmt.Sprintf for improve readability
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandear committed Oct 24, 2024
1 parent 4367bb9 commit ea64cbb
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
3 changes: 1 addition & 2 deletions codegen/directive.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package codegen

import (
"fmt"
"strconv"
"strings"

"github.com/vektah/gqlparser/v2/ast"
Expand Down Expand Up @@ -143,7 +142,7 @@ func (d *Directive) CallArgs() string {
args := []string{"ctx", "obj", "n"}

for _, arg := range d.Args {
args = append(args, "args["+strconv.Quote(arg.Name)+"].("+templates.CurrentImports.LookupType(arg.TypeReference.GO)+")")
args = append(args, fmt.Sprintf("args[%q].(%s)", arg.Name, templates.CurrentImports.LookupType(arg.TypeReference.GO)))
}

return strings.Join(args, ", ")
Expand Down
38 changes: 38 additions & 0 deletions codegen/directive_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package codegen

import (
"go/types"
"testing"

"github.com/stretchr/testify/assert"
"github.com/vektah/gqlparser/v2/ast"

"github.com/99designs/gqlgen/codegen/config"
)

func TestDirectiveCallArgs(t *testing.T) {
d := &Directive{
Args: []*FieldArgument{
{
ArgumentDefinition: &ast.ArgumentDefinition{
Name: "def1",
},
TypeReference: &config.TypeReference{
GO: types.Default(types.NewNamed(types.NewTypeName(0, nil, "string", nil), types.Typ[types.String], nil)),
},
},
{
ArgumentDefinition: &ast.ArgumentDefinition{
Name: "def2",
},
TypeReference: &config.TypeReference{
GO: types.Default(types.NewStruct(nil, nil)),
},
},
},
}

got := d.CallArgs()

assert.Equal(t, `ctx, obj, n, args["def1"].(string), args["def2"].(struct{})`, got)
}

0 comments on commit ea64cbb

Please sign in to comment.