|
| 1 | +{{ define "messages" }} |
| 2 | +{{- template "header" $ }} |
| 3 | + |
| 4 | +func indirectOf[P ~*T, T any](p P) (zero T) { |
| 5 | + return zero |
| 6 | +} |
| 7 | + |
| 8 | +func elemOf[S ~[]T, T any](s S) (zero T) { |
| 9 | + return zero |
| 10 | +} |
| 11 | + |
| 12 | +func valueOf[M ~map[K]V, K comparable, V any](m M) (zero V) { |
| 13 | + return zero |
| 14 | +} |
| 15 | + |
| 16 | +func initPtr[P ~*T, T any](p *P) { |
| 17 | + var zero T |
| 18 | + *p = &zero |
| 19 | +} |
| 20 | + |
| 21 | +func insertMap[M ~map[K]V, K comparable, V any](m *M, k K, v V) { |
| 22 | + if *m == nil { |
| 23 | + *m = make(M) |
| 24 | + } |
| 25 | + (*m)[k] = v |
| 26 | +} |
| 27 | + |
| 28 | +{{ range $m := $.Messages }} |
| 29 | +func (pm *{{ $m.ProtoType }}) FromOpenAPI(om {{ $m.OgenType }}) { |
| 30 | + {{- range $f := $m.Fields -}} |
| 31 | + {{ template "map_value_from" $f.Elem }} |
| 32 | + {{ end -}} |
| 33 | +} |
| 34 | + |
| 35 | +func (pm *{{ $m.ProtoType }}) ToOpenAPI() (om {{ $m.OgenType }}) { |
| 36 | + {{- range $f := $m.Fields -}} |
| 37 | + {{ template "map_value_to" $f.Elem }} |
| 38 | + {{ end -}} |
| 39 | + return om |
| 40 | +} |
| 41 | +{{ end }} |
| 42 | + |
| 43 | +{{ range $e := $.Enums }} |
| 44 | +func (pm *{{ $e.ProtoType }}) FromOpenAPI(om {{ $e.OgenType }}) { |
| 45 | + idx := {{ $e.EnumValueMap }}[string(om)] |
| 46 | + *pm = {{ $e.ProtoType }}(idx) |
| 47 | +} |
| 48 | + |
| 49 | +func (pm {{ $e.ProtoType }}) ToOpenAPI() {{ $e.OgenType }} { |
| 50 | + name := {{ $e.EnumNameMap }}[int32(pm)] |
| 51 | + return {{ $e.OgenType }}(name) |
| 52 | +} |
| 53 | +{{ end }} |
| 54 | + |
| 55 | +{{- end }} |
| 56 | + |
| 57 | + |
| 58 | +{{ define "map_value_to" }} |
| 59 | +{{ $t := $.Ogen }} |
| 60 | +{{- if $.CheckProtoForNil -}} if {{ $.ProtoSel }} != nil {{- end }} { |
| 61 | + {{- if $t.IsPointer }} |
| 62 | + ogenVal := indirectOf({{ $.OgenSel }}) |
| 63 | + {{- $elem := field_elem $t.PointerTo $.Proto "ogenVal" $.ProtoSel -}} |
| 64 | + {{ template "map_value_to" $elem }} |
| 65 | + {{ $.OgenSel }} = &ogenVal |
| 66 | + {{- else if $t.IsGeneric }} |
| 67 | + ogenVal := {{ $.OgenSel }}.Value |
| 68 | + {{- $elem := field_elem $t.GenericOf $.Proto "ogenVal" $.ProtoSel -}} |
| 69 | + {{ template "map_value_to" $elem }} |
| 70 | + {{ $.OgenSel }}.SetTo(ogenVal) |
| 71 | + {{- else if $t.IsArray }} |
| 72 | + for _, protoVal := range {{ $.ProtoSel }} { |
| 73 | + ogenElem := elemOf({{ $.OgenSel }}) |
| 74 | + |
| 75 | + {{- $elem := field_elem $t.Item $.Proto "ogenElem" "protoVal" -}} |
| 76 | + {{ template "map_value_to" $elem }} |
| 77 | + |
| 78 | + {{ $.OgenSel }} = append({{ $.OgenSel }}, ogenElem) |
| 79 | + } |
| 80 | + {{- else if $t.IsMap }} |
| 81 | + for key, protoVal := range {{ $.ProtoSel }} { |
| 82 | + ogenElem := valueOf({{ $.OgenSel }}) |
| 83 | + |
| 84 | + {{- $elem := field_elem $t.Item $.Proto "ogenElem" "protoVal" -}} |
| 85 | + {{ template "map_value_to" $elem }} |
| 86 | + |
| 87 | + insertMap(&{{ $.OgenSel }}, key, ogenElem) |
| 88 | + } |
| 89 | + {{- else if or ($t.IsStruct) ($t.IsAlias) }} |
| 90 | + {{ $.OgenSel }} = {{ $.ProtoSel }}.ToOpenAPI() |
| 91 | + {{- else if $t.IsEnum }} |
| 92 | + {{ $.OgenSel }} = {{ $.ProtoSel }}.ToOpenAPI() |
| 93 | + {{- else if $t.IsPrimitive }} |
| 94 | + {{- if $.Ptr }} |
| 95 | + {{ $.OgenSel }} = *{{ $.ProtoSel }} |
| 96 | + {{- else }} |
| 97 | + {{ $.OgenSel }} = {{ $.ProtoSel }} |
| 98 | + {{- end }} |
| 99 | + {{- else }} |
| 100 | + {{ errorf "unsupported kind: %s" $t.Kind }} |
| 101 | + {{- end }} |
| 102 | +} |
| 103 | +{{- end }} |
| 104 | + |
| 105 | +{{ define "map_value_from" }} |
| 106 | + {{- $t := $.Ogen }} |
| 107 | + {{- if $t.IsPointer }} |
| 108 | + if {{ $.OgenSel }} != nil { |
| 109 | + ogenVal := *{{ $.OgenSel }} |
| 110 | + {{- $elem := field_elem $t.PointerTo $.Proto "ogenVal" $.ProtoSel -}} |
| 111 | + {{ template "map_value_from" $elem }} |
| 112 | + } |
| 113 | + {{- else if $t.IsGeneric }} |
| 114 | + if ogenVal, ok := {{ $.OgenSel }}.Get(); ok { |
| 115 | + {{- $elem := field_elem $t.GenericOf $.Proto "ogenVal" $.ProtoSel -}} |
| 116 | + {{ template "map_value_from" $elem }} |
| 117 | + } |
| 118 | + {{- else if $t.IsArray }} |
| 119 | + for _, ogenVal := range {{ $.OgenSel }} { |
| 120 | + protoVal := elemOf({{ $.ProtoSel }}) |
| 121 | + |
| 122 | + {{- $elem := field_elem $t.Item $.Proto "ogenVal" "protoVal" -}} |
| 123 | + {{ template "map_value_from" $elem }} |
| 124 | + |
| 125 | + {{ $.ProtoSel }} = append({{ $.ProtoSel }}, protoVal) |
| 126 | + } |
| 127 | + {{- else if $t.IsMap }} |
| 128 | + for key, ogenVal := range {{ $.OgenSel }} { |
| 129 | + protoVal := valueOf({{ $.ProtoSel }}) |
| 130 | + |
| 131 | + {{- $elem := field_elem $t.Item $.Proto "ogenVal" "protoVal" -}} |
| 132 | + {{ template "map_value_from" $elem }} |
| 133 | + |
| 134 | + insertMap(&{{ $.ProtoSel }}, key, protoVal) |
| 135 | + } |
| 136 | + {{- else if or ($t.IsStruct) ($t.IsAlias) }} |
| 137 | + {{ $.ProtoSel }} = new({{ $.ProtoType }}) |
| 138 | + {{ $.ProtoSel }}.FromOpenAPI({{ $.OgenSel }}) |
| 139 | + {{- else if $t.IsEnum }} |
| 140 | + {{ $.ProtoSel }} = new({{ $.ProtoType }}) |
| 141 | + {{ $.ProtoSel }}.FromOpenAPI({{ $.OgenSel }}) |
| 142 | + {{- else if $t.IsPrimitive }} |
| 143 | + {{- if $.Ptr }} |
| 144 | + {{ $.ProtoSel }} = &{{ $.OgenSel }} |
| 145 | + {{- else }} |
| 146 | + {{ $.ProtoSel }} = {{ $.OgenSel }} |
| 147 | + {{- end }} |
| 148 | + {{- else }} |
| 149 | + {{ errorf "unsupported kind: %s" $t.Kind }} |
| 150 | + {{- end }} |
| 151 | +{{- end }} |
0 commit comments