Test case
1 generated file · +187 −0test/inputs/schema/enum.schema
Aschema-golangenum-type-name-suffix-true--6270b65ee01d / quicktype.go+187 −0
| @@ -0,0 +1,187 @@ | ||
| 1 | +// Code generated from JSON Schema using quicktype. DO NOT EDIT. | |
| 2 | +// To parse and unparse this JSON data, add this code to your project and do: | |
| 3 | +// | |
| 4 | +// topLevel, err := UnmarshalTopLevel(bytes) | |
| 5 | +// bytes, err = topLevel.Marshal() | |
| 6 | + | |
| 7 | +package main | |
| 8 | + | |
| 9 | +import "bytes" | |
| 10 | +import "errors" | |
| 11 | + | |
| 12 | +import "encoding/json" | |
| 13 | + | |
| 14 | +func UnmarshalTopLevel(data []byte) (TopLevel, error) { | |
| 15 | + var r TopLevel | |
| 16 | + err := json.Unmarshal(data, &r) | |
| 17 | + return r, err | |
| 18 | +} | |
| 19 | + | |
| 20 | +func (r *TopLevel) Marshal() ([]byte, error) { | |
| 21 | + return json.Marshal(r) | |
| 22 | +} | |
| 23 | + | |
| 24 | +type TopLevel struct { | |
| 25 | + Arr []Arr `json:"arr,omitempty"` | |
| 26 | + For *string `json:"for,omitempty"` | |
| 27 | + Gve Gve `json:"gve"` | |
| 28 | + Lvc *Lvc `json:"lvc,omitempty"` | |
| 29 | + OtherArr []OtherArr `json:"otherArr,omitempty"` | |
| 30 | +} | |
| 31 | + | |
| 32 | +type OtherArr string | |
| 33 | + | |
| 34 | +const ( | |
| 35 | + FooOtherArr OtherArr = "foo" | |
| 36 | + BarOtherArr OtherArr = "bar" | |
| 37 | + IfOtherArr OtherArr = "if" | |
| 38 | +) | |
| 39 | + | |
| 40 | +type Gve string | |
| 41 | + | |
| 42 | +const ( | |
| 43 | + GoodGve Gve = "good" | |
| 44 | + GveNeutralGve Gve = "neutral" | |
| 45 | + EvilGve Gve = "evil" | |
| 46 | +) | |
| 47 | + | |
| 48 | +type Lvc string | |
| 49 | + | |
| 50 | +const ( | |
| 51 | + LawfulLvc Lvc = "lawful" | |
| 52 | + LvcNeutralLvc Lvc = "neutral" | |
| 53 | + ChaoticLvc Lvc = "chaotic" | |
| 54 | +) | |
| 55 | + | |
| 56 | +type Arr struct { | |
| 57 | + Enum *OtherArr | |
| 58 | + Integer *int64 | |
| 59 | +} | |
| 60 | + | |
| 61 | +func (x *Arr) UnmarshalJSON(data []byte) error { | |
| 62 | + x.Enum = nil | |
| 63 | + object, err := unmarshalUnion(data, &x.Integer, nil, nil, nil, false, nil, false, nil, false, nil, true, &x.Enum, false) | |
| 64 | + if err != nil { | |
| 65 | + return err | |
| 66 | + } | |
| 67 | + if object { | |
| 68 | + } | |
| 69 | + return nil | |
| 70 | +} | |
| 71 | + | |
| 72 | +func (x *Arr) MarshalJSON() ([]byte, error) { | |
| 73 | + return marshalUnion(x.Integer, nil, nil, nil, false, nil, false, nil, false, nil, x.Enum != nil, x.Enum, false) | |
| 74 | +} | |
| 75 | + | |
| 76 | +func unmarshalUnion(data []byte, pi **int64, pf **float64, pb **bool, ps **string, haveArray bool, pa interface{}, haveObject bool, pc interface{}, haveMap bool, pm interface{}, haveEnum bool, pe interface{}, nullable bool) (bool, error) { | |
| 77 | + if pi != nil { | |
| 78 | + *pi = nil | |
| 79 | + } | |
| 80 | + if pf != nil { | |
| 81 | + *pf = nil | |
| 82 | + } | |
| 83 | + if pb != nil { | |
| 84 | + *pb = nil | |
| 85 | + } | |
| 86 | + if ps != nil { | |
| 87 | + *ps = nil | |
| 88 | + } | |
| 89 | + | |
| 90 | + dec := json.NewDecoder(bytes.NewReader(data)) | |
| 91 | + dec.UseNumber() | |
| 92 | + tok, err := dec.Token() | |
| 93 | + if err != nil { | |
| 94 | + return false, err | |
| 95 | + } | |
| 96 | + | |
| 97 | + switch v := tok.(type) { | |
| 98 | + case json.Number: | |
| 99 | + if pi != nil { | |
| 100 | + i, err := v.Int64() | |
| 101 | + if err == nil { | |
| 102 | + *pi = &i | |
| 103 | + return false, nil | |
| 104 | + } | |
| 105 | + } | |
| 106 | + if pf != nil { | |
| 107 | + f, err := v.Float64() | |
| 108 | + if err == nil { | |
| 109 | + *pf = &f | |
| 110 | + return false, nil | |
| 111 | + } | |
| 112 | + return false, errors.New("Unparsable number") | |
| 113 | + } | |
| 114 | + return false, errors.New("Union does not contain number") | |
| 115 | + case float64: | |
| 116 | + return false, errors.New("Decoder should not return float64") | |
| 117 | + case bool: | |
| 118 | + if pb != nil { | |
| 119 | + *pb = &v | |
| 120 | + return false, nil | |
| 121 | + } | |
| 122 | + return false, errors.New("Union does not contain bool") | |
| 123 | + case string: | |
| 124 | + if haveEnum { | |
| 125 | + return false, json.Unmarshal(data, pe) | |
| 126 | + } | |
| 127 | + if ps != nil { | |
| 128 | + *ps = &v | |
| 129 | + return false, nil | |
| 130 | + } | |
| 131 | + return false, errors.New("Union does not contain string") | |
| 132 | + case nil: | |
| 133 | + if nullable { | |
| 134 | + return false, nil | |
| 135 | + } | |
| 136 | + return false, errors.New("Union does not contain null") | |
| 137 | + case json.Delim: | |
| 138 | + if v == '{' { | |
| 139 | + if haveObject { | |
| 140 | + return true, json.Unmarshal(data, pc) | |
| 141 | + } | |
| 142 | + if haveMap { | |
| 143 | + return false, json.Unmarshal(data, pm) | |
| 144 | + } | |
| 145 | + return false, errors.New("Union does not contain object") | |
| 146 | + } | |
| 147 | + if v == '[' { | |
| 148 | + if haveArray { | |
| 149 | + return false, json.Unmarshal(data, pa) | |
| 150 | + } | |
| 151 | + return false, errors.New("Union does not contain array") | |
| 152 | + } | |
| 153 | + return false, errors.New("Cannot handle delimiter") | |
| 154 | + } | |
| 155 | + return false, errors.New("Cannot unmarshal union") | |
| 156 | +} | |
| 157 | + | |
| 158 | +func marshalUnion(pi *int64, pf *float64, pb *bool, ps *string, haveArray bool, pa interface{}, haveObject bool, pc interface{}, haveMap bool, pm interface{}, haveEnum bool, pe interface{}, nullable bool) ([]byte, error) { | |
| 159 | + if pi != nil { | |
| 160 | + return json.Marshal(*pi) | |
| 161 | + } | |
| 162 | + if pf != nil { | |
| 163 | + return json.Marshal(*pf) | |
| 164 | + } | |
| 165 | + if pb != nil { | |
| 166 | + return json.Marshal(*pb) | |
| 167 | + } | |
| 168 | + if ps != nil { | |
| 169 | + return json.Marshal(*ps) | |
| 170 | + } | |
| 171 | + if haveArray { | |
| 172 | + return json.Marshal(pa) | |
| 173 | + } | |
| 174 | + if haveObject { | |
| 175 | + return json.Marshal(pc) | |
| 176 | + } | |
| 177 | + if haveMap { | |
| 178 | + return json.Marshal(pm) | |
| 179 | + } | |
| 180 | + if haveEnum { | |
| 181 | + return json.Marshal(pe) | |
| 182 | + } | |
| 183 | + if nullable { | |
| 184 | + return json.Marshal(nil) | |
| 185 | + } | |
| 186 | + return nil, errors.New("Union must not be null") | |
| 187 | +} |
No generated files match these filters.