@@ -87,8 +87,9 @@ See https://github.com/thnxdev/happy for more information.
87
87
didWork = true
88
88
89
89
gctx := & genContext {
90
- Writer : codewriter .New (pkg .Name ),
91
- pkg : pkg ,
90
+ Writer : codewriter .New (pkg .Name ),
91
+ pkg : pkg ,
92
+ haveDecoder : map [string ]bool {},
92
93
}
93
94
gctx .Import ("net/http" , "io" , "encoding/json" , "strconv" )
94
95
for _ , svcEndpoints := range endpoints {
@@ -352,6 +353,10 @@ func genQueryDecoderFunc(gctx *genContext, paramType types.Type) (name string, e
352
353
gctx .Import ("net/url" )
353
354
_ , typeRef := gctx .TypeRef (paramType )
354
355
name = "decode" + ucFirst (strings .ReplaceAll (typeRef , "." , "" ))
356
+ if gctx .haveDecoder [typeRef ] {
357
+ return name , nil
358
+ }
359
+ gctx .haveDecoder [typeRef ] = true
355
360
w := gctx .Trailer ()
356
361
w .L ("func %s(p url.Values, out *%s) (err error) {" , name , typeRef )
357
362
w = w .Push ()
@@ -374,6 +379,7 @@ func genQueryDecoderFunc(gctx *genContext, paramType types.Type) (name string, e
374
379
w .L ("out.%s = new(%s)" , field .Name (), fieldType )
375
380
strctRef = "*" + strctRef
376
381
}
382
+ w .Import ("fmt" )
377
383
switch fieldType .String () {
378
384
case "time.Duration" :
379
385
gctx .Import ("time" )
@@ -423,6 +429,7 @@ func lcFirst(s string) string {
423
429
type genContext struct {
424
430
pkg * packages.Package
425
431
* codewriter.Writer
432
+ haveDecoder map [string ]bool
426
433
}
427
434
428
435
func (g * genContext ) Pos (pos token.Pos ) token.Position {
@@ -536,7 +543,7 @@ func genEndpoint(gctx *genContext, w *codewriter.Writer, ep endpoint) error {
536
543
return fmt .Errorf ("%s: %w" , pos , err )
537
544
}
538
545
w .L ("if err := %s(r.URL.Query(), ¶m%d); err != nil {" , decoderFn , i )
539
- w .L (` http.Error(w, fmt.Sprintf("Failed to decode query parameters: %%s", err ), http.StatusBadRequest)` )
546
+ w .L (` http.Error(w, err.Error( ), http.StatusBadRequest)` )
540
547
w .L (" return" )
541
548
w .L ("}" )
542
549
} else {
0 commit comments