Skip to content

Commit

Permalink
Fix "fmt" import (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
nehzata authored Oct 26, 2023
1 parent 03e5a17 commit a851a26
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion cmd/happy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,27 +379,30 @@ func genQueryDecoderFunc(gctx *genContext, paramType types.Type) (name string, e
w.L("out.%s = new(%s)", field.Name(), fieldType)
strctRef = "*" + strctRef
}
w.Import("fmt")
switch fieldType.String() {
case "time.Duration":
gctx.Import("time")
w.Import("fmt")
w.L("if %s.%s, err = time.ParseDuration(q[len(q)-1]); err != nil {", strctRef, field.Name())
w.L(` return fmt.Errorf("failed to decode query parameter \"%s\" as %s: %%w", err)`, fieldName, fieldType)
w.L("}")
case "bool":
gctx.Import("strconv")
w.Import("fmt")
w.L("if %s.%s, err = strconv.ParseBool(q[len(q)-1]); err != nil {", strctRef, field.Name())
w.L(` return fmt.Errorf("failed to decode query parameter \"%s\" as %s: %%w", err)`, fieldName, fieldType)
w.L("}")
case "int":
gctx.Import("strconv")
w.Import("fmt")
w.L("if %s.%s, err = strconv.Atoi(q[len(q)-1]); err != nil {", strctRef, field.Name())
w.L(` return fmt.Errorf("failed to decode query parameter \"%s\" as %s: %%w", err)`, fieldName, fieldType)
w.L("}")
case "string":
w.L("%s.%s = q[len(q)-1]", strctRef, field.Name())
default:
if implements(field, textUnmarshalerInterface()) {
w.Import("fmt")
w.L("if err = %s.%s.UnmarshalText([]byte(q[len(q)-1])); err != nil {", strctRef, field.Name())
w.L(` return fmt.Errorf("failed to decode query parameter \"%s\" as %s: %%w", err)`, fieldName, fieldType)
w.L("}")
Expand Down

0 comments on commit a851a26

Please sign in to comment.