Skip to content

Commit

Permalink
Merge pull request #1334 from abemedia/feat/time-format
Browse files Browse the repository at this point in the history
feat(gen): support custom time format
  • Loading branch information
tdakkota authored Oct 25, 2024
2 parents 3c2d14c + dbd90b0 commit 7e342ba
Show file tree
Hide file tree
Showing 36 changed files with 2,634 additions and 111 deletions.
102 changes: 102 additions & 0 deletions _testdata/positive/time_extension.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
openapi: 3.0.3
info:
title: API
version: 0.1.0
paths:
/optional:
get:
operationId: default
parameters:
- name: date
in: query
schema:
type: string
format: date
x-ogen-time-format: 02/01/2006
default: 04/03/2001
- name: time
in: query
schema:
type: string
format: time
x-ogen-time-format: 3:04PM
default: 1:23AM
- name: dateTime
in: query
schema:
type: string
format: date-time
x-ogen-time-format: 2006-01-02T15:04:05.999999999Z07:00
default: 2001-03-04T01:23:45.123456789-07:00
responses:
'200':
description: Test
content:
application/json:
schema:
type: object
properties:
date:
type: string
format: date
x-ogen-time-format: 02/01/2006
default: 04/03/2001
time:
type: string
format: time
x-ogen-time-format: 3:04PM
default: 1:23AM
dateTime:
type: string
format: date-time
x-ogen-time-format: 2006-01-02T15:04:05.999999999Z07:00
default: 2001-03-04T01:23:45.123456789-07:00
/required:
get:
operationId: required
parameters:
- name: date
in: query
required: true
schema:
type: string
format: date
x-ogen-time-format: 02/01/2006
- name: time
in: query
required: true
schema:
type: string
format: time
x-ogen-time-format: 3:04PM
- name: dateTime
in: query
required: true
schema:
type: string
format: date-time
x-ogen-time-format: 2006-01-02T15:04:05.999999999Z07:00
responses:
'200':
description: Test
content:
application/json:
schema:
type: object
required:
- date
- time
- dateTime
properties:
date:
type: string
format: date
x-ogen-time-format: 02/01/2006
time:
type: string
format: time
x-ogen-time-format: 3:04PM
dateTime:
type: string
format: date
x-ogen-time-format: 2006-01-02T15:04:05.999999999Z07:00
6 changes: 3 additions & 3 deletions gen/_template/client.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func NewWebhookClient(opts ...ClientOption) (*WebhookClient, error) {
}

{{- range $op := $ops }}
{{ template "client/operation" op_elem $op $ }}
{{ template "client/operation" op_elem $op $ }}
{{- end }}

{{- end }}
Expand Down Expand Up @@ -273,7 +273,7 @@ func (c *{{ if $op.WebhookInfo }}Webhook{{ end }}Client) send{{ $op.Name }}(ctx
{{ if $op.HasCookieParams }}
{{ if $otel }}stage = "EncodeCookieParams"{{ end }}
{{- template "encode_cookie_parameters" $op }}
{{- end }}
{{- end }}

{{- with $securities := $op.Security.Securities }}
{
Expand All @@ -299,7 +299,7 @@ func (c *{{ if $op.WebhookInfo }}Webhook{{ end }}Client) send{{ $op.Name }}(ctx
for _, requirement := range []bitset{
{{- range $req := $op.Security.Requirements }}
{
{{- range $mask := $req }}{{ printf "%#08b" $mask }},{{ end -}}
{{- range $mask := $req }}{{ printf "%#08b" $mask }},{{ end -}}
},
{{- end }}
} {
Expand Down
6 changes: 4 additions & 2 deletions gen/_template/defaults/set.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
{{ $.Var }}.SetTo(val)
{{- end }}
{{- else if $t.IsPointer -}}
{{- template "defaults/val" default_elem $t.PointerTo $.Var $.Default }}
{{- template "defaults/val" default_elem $t.PointerTo $.Var $.Default }}
{{ $.Var }} = &val
{{- else -}}
{{ errorf "unsupported %#v: %s" $.Default.Value $t }}
Expand All @@ -26,7 +26,9 @@
{{- define "defaults/val" -}}

{{ $t := $.Type }}{{ $j := $t.JSON }}{{- $val := print_go $.Default.Value }}
{{- if $j.Format }}
{{- if $j.TimeFormat }}
val, _ := json.DecodeTimeFormat(jx.DecodeStr({{ quote $val }}), {{ $j.TimeFormat }})
{{- else if $j.Format }}
val, _ := json.Decode{{ $j.Format }}(jx.DecodeStr({{ quote $val }}))
{{- else if $j.IsBase64 }}
val, _ := jx.DecodeStr({{ quote $val }}).Base64()
Expand Down
2 changes: 1 addition & 1 deletion gen/_template/faker.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{{ template "header" $ }}

{{- range $_, $s := $.Types }}{{- if $s.HasFeature "json" }}
{{ template "faker/fakers" $s }}
{{ template "faker/fakers" $s }}
{{- end }}{{- end }}

{{- end }}
6 changes: 3 additions & 3 deletions gen/_template/handlers.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func recordError(string, error) {}

{{- if $.WebhookServerEnabled }}
{{- range $op := $.Webhooks }}
{{- template "handlers/operation" op_elem $op $ }}
{{- template "handlers/operation" op_elem $op $ }}
{{ end }}
{{- end }}

Expand Down Expand Up @@ -128,7 +128,7 @@ func (s *{{ if $op.WebhookInfo }}Webhook{{ end }}Server) handle{{ $op.Name }}Req
for _, requirement := range []bitset{
{{- range $req := $op.Security.Requirements }}
{
{{- range $mask := $req }}{{ printf "%#08b" $mask }},{{ end }}
{{- range $mask := $req }}{{ printf "%#08b" $mask }},{{ end }}
},
{{- end }}
} {
Expand Down Expand Up @@ -156,7 +156,7 @@ func (s *{{ if $op.WebhookInfo }}Webhook{{ end }}Server) handle{{ $op.Name }}Req
return
}
}
{{- end }}
{{- end }}

{{- if $op.Params }}
params, err := decode{{ $op.Name }}Params(args, argsEscaped, r)
Expand Down
2 changes: 1 addition & 1 deletion gen/_template/json.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
{{- range $_, $t := $.Types }}
{{- if $t.HasFeature "json" }}
{{- template "json/encoders" $t }}
{{- template "json/stdmarshaler" $t }}
{{- template "json/stdmarshaler" $t }}
{{- end }}
{{- end }}
{{ end }}
Expand Down
26 changes: 19 additions & 7 deletions gen/_template/json/decode.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,15 @@ if err := d.Arr(func(d *jx.Decoder) error {
{{ $.Var }}.Reset()
{{- end }}
{{- if $g.Format }}
if err := {{ $.Var }}.Decode(d, json.Decode{{ $g.JSON.Format }}); err != nil {
return err
}
{{ if $g.JSON.TimeFormat -}}
if err := {{ $.Var }}.Decode(d, json.NewTimeDecoder({{ $g.JSON.TimeFormat }})); err != nil {
return err
}
{{- else -}}
if err := {{ $.Var }}.Decode(d, json.Decode{{ $g.JSON.Format }}); err != nil {
return err
}
{{- end }}
{{- else }}
if err := {{ $.Var }}.Decode(d); err != nil {
return err
Expand All @@ -60,8 +66,8 @@ if err := d.Arr(func(d *jx.Decoder) error {
{{- $j := $t.JSON -}}
{{- if $t.IsPointer }}
{{- template "json/dec_pointer" $ }}
{{- else if $t.IsGeneric }}
{{- template "json/dec_generic" $ }}
{{- else if $t.IsGeneric }}
{{- template "json/dec_generic" $ }}
{{- else if $t.IsArray }}
{{ template "json/dec_array" $ }}
{{- else if $t.IsMap }}
Expand All @@ -72,10 +78,16 @@ if err := d.Arr(func(d *jx.Decoder) error {
if err := {{ $.Var }}.Decode(d); err != nil {
return err
}
{{- else if $t.IsNull }}
{{- else if $t.IsNull }}
if err := d.Null(); err != nil {
return err
}
{{- else if $j.TimeFormat }}
v, err := json.DecodeTimeFormat(d, {{ $j.TimeFormat }})
{{ $.Var }} = v
if err != nil {
return err
}
{{- else if $j.Format }}
v, err := json.Decode{{ $j.Format }}(d)
{{ $.Var }} = v
Expand All @@ -89,6 +101,6 @@ if err := d.Arr(func(d *jx.Decoder) error {
return err
}
{{- else }}
{{ errorf "unsupported kind: %s" $t.Kind }}
{{ errorf "unsupported kind: %s" $t.Kind }}
{{- end }}
{{- end -}}
25 changes: 16 additions & 9 deletions gen/_template/json/encode.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ if {{ $.Var }}.Set {
{{- template "json/enc_generic_field" $ }}
}
{{- else }}
{{- template "json/enc_generic_field" $ }}
{{- template "json/enc_generic_field" $ }}
{{- end -}}

{{- end }}
Expand All @@ -28,9 +28,13 @@ if {{ $.Var }}.Set {

{{- template "json/enc_field" $ }}
{{- if $g.Format }}
{{ $.Var }}.Encode(e, json.Encode{{ $g.JSON.Format }})
{{ if $g.JSON.TimeFormat -}}
{{ $.Var }}.Encode(e, json.NewTimeEncoder({{ $g.JSON.TimeFormat }}))
{{- else -}}
{{ $.Var }}.Encode(e, json.Encode{{ $g.JSON.Format }})
{{- end }}
{{- else }}
{{ $.Var }}.Encode(e)
{{ $.Var }}.Encode(e)
{{- end }}

{{- end }}
Expand Down Expand Up @@ -98,17 +102,20 @@ if {{ $.Var }}.Set {
{{- template "json/enc_generic" $ }}
{{- else if $t.IsArray -}}
{{- template "json/enc_array" $ }}
{{- else if $t.IsAny }}
{{- else if $t.IsAny }}
if len({{ $.Var }}) != 0 {
{{- template "json/enc_field" $ }}
{{- template "json/enc_field" $ }}
e.{{ $j.Fn }}({{ $.Var }})
}
{{- else if $t.IsNull }}
{{- else if $t.IsNull }}
_ = {{ $.Var }}
{{- template "json/enc_field" $ }}
{{- template "json/enc_field" $ }}
e.Null()
{{- else if $j.Format -}}
{{- template "json/enc_field" $ }}
{{- else if $j.TimeFormat }}
{{- template "json/enc_field" $ }}
json.EncodeTimeFormat(e, {{ $.Var }}, {{ $j.TimeFormat }})
{{- else if $j.Format -}}
{{- template "json/enc_field" $ }}
json.Encode{{ $j.Format }}(e, {{ $.Var }})
{{- else if $j.Fn -}}
{{- template "json/enc_field" $ }}
Expand Down
10 changes: 9 additions & 1 deletion gen/_template/json/encoders_generic.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ func (o {{ $.ReadOnlyReceiver }}) Encode(e *jx.Encoder{{ if $g.Format }}, format
{{- end }}
{{- if $g.Format }}
format(e, o.Value)
{{- else if $g.JSON.TimeFormat }}
json.EncodeTimeFormat(e, o.Value, {{ $g.JSON.TimeFormat }})
{{- else if $g.JSON.Format }}
json.Encode{{ $g.JSON.Format }}(e, o.Value)
{{- else if $g.JSON.Fn }}
Expand All @@ -31,7 +33,7 @@ func (o {{ $.ReadOnlyReceiver }}) Encode(e *jx.Encoder{{ if $g.Format }}, format
{{- else if or ($g.IsStruct) ($g.IsMap) ($g.IsEnum) ($g.IsPointer) ($g.IsSum) ($g.IsAlias) }}
o.Value.Encode(e)
{{- else }}
{{ errorf "unexpected kind %s" $g.Kind }}
{{ errorf "unexpected kind %s" $g.Kind }}
{{- end }}
}

Expand Down Expand Up @@ -68,6 +70,12 @@ func (o *{{ $.Name }}) Decode(d *jx.Decoder{{ if $g.Format }}, format func(*jx.D
return err
}
o.Value = v
{{- else if $g.JSON.TimeFormat }}
v, err := json.DecodeTimeFormat(d, {{ $g.JSON.TimeFormat }})
if err != nil {
return err
}
o.Value = v
{{- else if $g.JSON.Format }}
v, err := json.Decode{{ $g.JSON.Format }}(d)
if err != nil {
Expand Down
12 changes: 10 additions & 2 deletions gen/_template/json/stdmarshaler.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ func (s {{ $.ReadOnlyReceiver }}) MarshalJSON() ([]byte, error) {
e := jx.Encoder{}
{{- if $.IsGeneric }}
{{- if $g.Format }}
s.Encode(&e, json.Encode{{ $g.JSON.Format }})
{{ if $g.JSON.TimeFormat -}}
s.Encode(&e, json.NewTimeEncoder({{ $g.JSON.TimeFormat }}))
{{- else -}}
s.Encode(&e, json.Encode{{ $g.JSON.Format }})
{{- end }}
{{- else }}
s.Encode(&e)
{{- end }}
Expand All @@ -21,7 +25,11 @@ func (s *{{ $.Name }}) UnmarshalJSON(data []byte) error {
d := jx.DecodeBytes(data)
{{- if $.IsGeneric }}
{{- if $g.Format }}
return s.Decode(d, json.Decode{{ $g.JSON.Format }})
{{ if $g.JSON.TimeFormat -}}
return s.Decode(d, json.NewTimeDecoder({{ $g.JSON.TimeFormat }}))
{{- else -}}
return s.Decode(d, json.Decode{{ $g.JSON.Format }})
{{- end }}
{{- else }}
return s.Decode(d)
{{- end }}
Expand Down
6 changes: 5 additions & 1 deletion gen/_template/uri/decode.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@
return err
}

c, err := conv.{{ $t.FromString }}(val)
{{ if $t.JSON.TimeFormat -}}
c, err := time.Parse({{ $t.JSON.TimeFormat }}, val)
{{- else -}}
c, err := conv.{{ $t.FromString }}(val)
{{- end }}
if err != nil {
return err
}
Expand Down
6 changes: 5 additions & 1 deletion gen/_template/uri/encode.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
{{- $t := $.Type }}
{{- $var := $.Var }}
{{- if $t.IsPrimitive }}
return e.EncodeValue(conv.{{ $t.ToString }}({{ $var }}))
{{ if $t.JSON.TimeFormat -}}
return e.EncodeValue({{ $var }}.Format({{ $t.JSON.TimeFormat }}))
{{- else -}}
return e.EncodeValue(conv.{{ $t.ToString }}({{ $var }}))
{{- end }}
{{- else if $t.IsEnum }}
return e.EncodeValue(conv.{{ $t.ToString }}({{ $t.Primitive.String }}({{ $var }})))
{{- else if $t.IsArray }}
Expand Down
Loading

0 comments on commit 7e342ba

Please sign in to comment.