github.com/confluentinc/confluent-kafka-go@v1.9.2/schemaregistry/test/demo_schema.go (about) 1 // Code generated by github.com/actgardner/gogen-avro/v10. DO NOT EDIT. 2 /* 3 * SOURCE: 4 * example.avsc 5 */ 6 package test 7 8 import ( 9 "encoding/json" 10 "fmt" 11 "io" 12 13 "github.com/actgardner/gogen-avro/v10/compiler" 14 "github.com/actgardner/gogen-avro/v10/vm" 15 "github.com/actgardner/gogen-avro/v10/vm/types" 16 ) 17 18 var _ = fmt.Printf 19 20 type DemoSchema struct { 21 IntField int32 `json:"IntField"` 22 23 DoubleField float64 `json:"DoubleField"` 24 25 StringField string `json:"StringField"` 26 27 BoolField bool `json:"BoolField"` 28 29 BytesField Bytes `json:"BytesField"` 30 } 31 32 const DemoSchemaAvroCRC64Fingerprint = "\xc4V\xa9\x04ʛf\xad" 33 34 func NewDemoSchema() DemoSchema { 35 r := DemoSchema{} 36 return r 37 } 38 39 func DeserializeDemoSchema(r io.Reader) (DemoSchema, error) { 40 t := NewDemoSchema() 41 deser, err := compiler.CompileSchemaBytes([]byte(t.Schema()), []byte(t.Schema())) 42 if err != nil { 43 return t, err 44 } 45 46 err = vm.Eval(r, deser, &t) 47 return t, err 48 } 49 50 func DeserializeDemoSchemaFromSchema(r io.Reader, schema string) (DemoSchema, error) { 51 t := NewDemoSchema() 52 53 deser, err := compiler.CompileSchemaBytes([]byte(schema), []byte(t.Schema())) 54 if err != nil { 55 return t, err 56 } 57 58 err = vm.Eval(r, deser, &t) 59 return t, err 60 } 61 62 func writeDemoSchema(r DemoSchema, w io.Writer) error { 63 var err error 64 err = vm.WriteInt(r.IntField, w) 65 if err != nil { 66 return err 67 } 68 err = vm.WriteDouble(r.DoubleField, w) 69 if err != nil { 70 return err 71 } 72 err = vm.WriteString(r.StringField, w) 73 if err != nil { 74 return err 75 } 76 err = vm.WriteBool(r.BoolField, w) 77 if err != nil { 78 return err 79 } 80 err = vm.WriteBytes(r.BytesField, w) 81 if err != nil { 82 return err 83 } 84 return err 85 } 86 87 func (r DemoSchema) Serialize(w io.Writer) error { 88 return writeDemoSchema(r, w) 89 } 90 91 func (r DemoSchema) Schema() string { 92 return "{\"fields\":[{\"name\":\"IntField\",\"type\":\"int\"},{\"name\":\"DoubleField\",\"type\":\"double\"},{\"name\":\"StringField\",\"type\":\"string\"},{\"name\":\"BoolField\",\"type\":\"boolean\"},{\"name\":\"BytesField\",\"type\":\"bytes\"}],\"name\":\"DemoSchema\",\"type\":\"record\"}" 93 } 94 95 func (r DemoSchema) SchemaName() string { 96 return "DemoSchema" 97 } 98 99 func (_ DemoSchema) SetBoolean(v bool) { panic("Unsupported operation") } 100 func (_ DemoSchema) SetInt(v int32) { panic("Unsupported operation") } 101 func (_ DemoSchema) SetLong(v int64) { panic("Unsupported operation") } 102 func (_ DemoSchema) SetFloat(v float32) { panic("Unsupported operation") } 103 func (_ DemoSchema) SetDouble(v float64) { panic("Unsupported operation") } 104 func (_ DemoSchema) SetBytes(v []byte) { panic("Unsupported operation") } 105 func (_ DemoSchema) SetString(v string) { panic("Unsupported operation") } 106 func (_ DemoSchema) SetUnionElem(v int64) { panic("Unsupported operation") } 107 108 func (r *DemoSchema) Get(i int) types.Field { 109 switch i { 110 case 0: 111 w := types.Int{Target: &r.IntField} 112 113 return w 114 115 case 1: 116 w := types.Double{Target: &r.DoubleField} 117 118 return w 119 120 case 2: 121 w := types.String{Target: &r.StringField} 122 123 return w 124 125 case 3: 126 w := types.Boolean{Target: &r.BoolField} 127 128 return w 129 130 case 4: 131 w := BytesWrapper{Target: &r.BytesField} 132 133 return w 134 135 } 136 panic("Unknown field index") 137 } 138 139 func (r *DemoSchema) SetDefault(i int) { 140 switch i { 141 } 142 panic("Unknown field index") 143 } 144 145 func (r *DemoSchema) NullField(i int) { 146 switch i { 147 } 148 panic("Not a nullable field index") 149 } 150 151 func (_ DemoSchema) AppendMap(key string) types.Field { panic("Unsupported operation") } 152 func (_ DemoSchema) AppendArray() types.Field { panic("Unsupported operation") } 153 func (_ DemoSchema) HintSize(int) { panic("Unsupported operation") } 154 func (_ DemoSchema) Finalize() {} 155 156 func (_ DemoSchema) AvroCRC64Fingerprint() []byte { 157 return []byte(DemoSchemaAvroCRC64Fingerprint) 158 } 159 160 func (r DemoSchema) MarshalJSON() ([]byte, error) { 161 var err error 162 output := make(map[string]json.RawMessage) 163 output["IntField"], err = json.Marshal(r.IntField) 164 if err != nil { 165 return nil, err 166 } 167 output["DoubleField"], err = json.Marshal(r.DoubleField) 168 if err != nil { 169 return nil, err 170 } 171 output["StringField"], err = json.Marshal(r.StringField) 172 if err != nil { 173 return nil, err 174 } 175 output["BoolField"], err = json.Marshal(r.BoolField) 176 if err != nil { 177 return nil, err 178 } 179 output["BytesField"], err = json.Marshal(r.BytesField) 180 if err != nil { 181 return nil, err 182 } 183 return json.Marshal(output) 184 } 185 186 func (r *DemoSchema) UnmarshalJSON(data []byte) error { 187 var fields map[string]json.RawMessage 188 if err := json.Unmarshal(data, &fields); err != nil { 189 return err 190 } 191 192 var val json.RawMessage 193 val = func() json.RawMessage { 194 if v, ok := fields["IntField"]; ok { 195 return v 196 } 197 return nil 198 }() 199 200 if val != nil { 201 if err := json.Unmarshal([]byte(val), &r.IntField); err != nil { 202 return err 203 } 204 } else { 205 return fmt.Errorf("no value specified for IntField") 206 } 207 val = func() json.RawMessage { 208 if v, ok := fields["DoubleField"]; ok { 209 return v 210 } 211 return nil 212 }() 213 214 if val != nil { 215 if err := json.Unmarshal([]byte(val), &r.DoubleField); err != nil { 216 return err 217 } 218 } else { 219 return fmt.Errorf("no value specified for DoubleField") 220 } 221 val = func() json.RawMessage { 222 if v, ok := fields["StringField"]; ok { 223 return v 224 } 225 return nil 226 }() 227 228 if val != nil { 229 if err := json.Unmarshal([]byte(val), &r.StringField); err != nil { 230 return err 231 } 232 } else { 233 return fmt.Errorf("no value specified for StringField") 234 } 235 val = func() json.RawMessage { 236 if v, ok := fields["BoolField"]; ok { 237 return v 238 } 239 return nil 240 }() 241 242 if val != nil { 243 if err := json.Unmarshal([]byte(val), &r.BoolField); err != nil { 244 return err 245 } 246 } else { 247 return fmt.Errorf("no value specified for BoolField") 248 } 249 val = func() json.RawMessage { 250 if v, ok := fields["BytesField"]; ok { 251 return v 252 } 253 return nil 254 }() 255 256 if val != nil { 257 if err := json.Unmarshal([]byte(val), &r.BytesField); err != nil { 258 return err 259 } 260 } else { 261 return fmt.Errorf("no value specified for BytesField") 262 } 263 return nil 264 }