Skip to content

Commit b446b64

Browse files
committed
feat: support decoding query parameters with encoding.TextUnmarshaler
1 parent 6714a29 commit b446b64

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

cmd/happy/main.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,13 @@ func genQueryDecoderFunc(gctx *genContext, paramType types.Type) (name string, e
393393
case "string":
394394
w.L("%s.%s = q[len(q)-1]", strctRef, field.Name())
395395
default:
396-
return "", fmt.Errorf("can't decode query parameter into field %s.%s of type %s, only time.Duration, int, string and bool are supported", paramType, field.Name(), field.Type())
396+
if implements(field, textUnmarshalerInterface()) {
397+
w.L("if err = %s.%s.UnmarshalText([]byte(q[len(q)-1])); err != nil {", strctRef, field.Name())
398+
w.L(` return fmt.Errorf("failed to decode query parameter \"%s\" as %s: %%w", err)`, fieldName, fieldType)
399+
w.L("}")
400+
} else {
401+
return "", fmt.Errorf("can't decode query parameter into field %s.%s of type %s, only encoding.TextUnmarshaler, time.Duration, int, string and bool are supported", paramType, field.Name(), field.Type())
402+
}
397403
}
398404
w = w.Pop()
399405
w.L("}")

0 commit comments

Comments
 (0)