github.com/confluentinc/confluent-kafka-go@v1.9.2/schemaregistry/test/nested_record.go (about)

     1  // Code generated by github.com/actgardner/gogen-avro/v10. DO NOT EDIT.
     2  /*
     3   * SOURCE:
     4   *     nested.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 NestedRecord struct {
    21  	StringField string `json:"StringField"`
    22  
    23  	BoolField bool `json:"BoolField"`
    24  
    25  	BytesField Bytes `json:"BytesField"`
    26  }
    27  
    28  const NestedRecordAvroCRC64Fingerprint = "\x81\x8d\xc3K?\xe83\xcc"
    29  
    30  func NewNestedRecord() NestedRecord {
    31  	r := NestedRecord{}
    32  	return r
    33  }
    34  
    35  func DeserializeNestedRecord(r io.Reader) (NestedRecord, error) {
    36  	t := NewNestedRecord()
    37  	deser, err := compiler.CompileSchemaBytes([]byte(t.Schema()), []byte(t.Schema()))
    38  	if err != nil {
    39  		return t, err
    40  	}
    41  
    42  	err = vm.Eval(r, deser, &t)
    43  	return t, err
    44  }
    45  
    46  func DeserializeNestedRecordFromSchema(r io.Reader, schema string) (NestedRecord, error) {
    47  	t := NewNestedRecord()
    48  
    49  	deser, err := compiler.CompileSchemaBytes([]byte(schema), []byte(t.Schema()))
    50  	if err != nil {
    51  		return t, err
    52  	}
    53  
    54  	err = vm.Eval(r, deser, &t)
    55  	return t, err
    56  }
    57  
    58  func writeNestedRecord(r NestedRecord, w io.Writer) error {
    59  	var err error
    60  	err = vm.WriteString(r.StringField, w)
    61  	if err != nil {
    62  		return err
    63  	}
    64  	err = vm.WriteBool(r.BoolField, w)
    65  	if err != nil {
    66  		return err
    67  	}
    68  	err = vm.WriteBytes(r.BytesField, w)
    69  	if err != nil {
    70  		return err
    71  	}
    72  	return err
    73  }
    74  
    75  func (r NestedRecord) Serialize(w io.Writer) error {
    76  	return writeNestedRecord(r, w)
    77  }
    78  
    79  func (r NestedRecord) Schema() string {
    80  	return "{\"fields\":[{\"name\":\"StringField\",\"type\":\"string\"},{\"name\":\"BoolField\",\"type\":\"boolean\"},{\"name\":\"BytesField\",\"type\":\"bytes\"}],\"name\":\"NestedRecord\",\"type\":\"record\"}"
    81  }
    82  
    83  func (r NestedRecord) SchemaName() string {
    84  	return "NestedRecord"
    85  }
    86  
    87  func (_ NestedRecord) SetBoolean(v bool)    { panic("Unsupported operation") }
    88  func (_ NestedRecord) SetInt(v int32)       { panic("Unsupported operation") }
    89  func (_ NestedRecord) SetLong(v int64)      { panic("Unsupported operation") }
    90  func (_ NestedRecord) SetFloat(v float32)   { panic("Unsupported operation") }
    91  func (_ NestedRecord) SetDouble(v float64)  { panic("Unsupported operation") }
    92  func (_ NestedRecord) SetBytes(v []byte)    { panic("Unsupported operation") }
    93  func (_ NestedRecord) SetString(v string)   { panic("Unsupported operation") }
    94  func (_ NestedRecord) SetUnionElem(v int64) { panic("Unsupported operation") }
    95  
    96  func (r *NestedRecord) Get(i int) types.Field {
    97  	switch i {
    98  	case 0:
    99  		w := types.String{Target: &r.StringField}
   100  
   101  		return w
   102  
   103  	case 1:
   104  		w := types.Boolean{Target: &r.BoolField}
   105  
   106  		return w
   107  
   108  	case 2:
   109  		w := BytesWrapper{Target: &r.BytesField}
   110  
   111  		return w
   112  
   113  	}
   114  	panic("Unknown field index")
   115  }
   116  
   117  func (r *NestedRecord) SetDefault(i int) {
   118  	switch i {
   119  	}
   120  	panic("Unknown field index")
   121  }
   122  
   123  func (r *NestedRecord) NullField(i int) {
   124  	switch i {
   125  	}
   126  	panic("Not a nullable field index")
   127  }
   128  
   129  func (_ NestedRecord) AppendMap(key string) types.Field { panic("Unsupported operation") }
   130  func (_ NestedRecord) AppendArray() types.Field         { panic("Unsupported operation") }
   131  func (_ NestedRecord) HintSize(int)                     { panic("Unsupported operation") }
   132  func (_ NestedRecord) Finalize()                        {}
   133  
   134  func (_ NestedRecord) AvroCRC64Fingerprint() []byte {
   135  	return []byte(NestedRecordAvroCRC64Fingerprint)
   136  }
   137  
   138  func (r NestedRecord) MarshalJSON() ([]byte, error) {
   139  	var err error
   140  	output := make(map[string]json.RawMessage)
   141  	output["StringField"], err = json.Marshal(r.StringField)
   142  	if err != nil {
   143  		return nil, err
   144  	}
   145  	output["BoolField"], err = json.Marshal(r.BoolField)
   146  	if err != nil {
   147  		return nil, err
   148  	}
   149  	output["BytesField"], err = json.Marshal(r.BytesField)
   150  	if err != nil {
   151  		return nil, err
   152  	}
   153  	return json.Marshal(output)
   154  }
   155  
   156  func (r *NestedRecord) UnmarshalJSON(data []byte) error {
   157  	var fields map[string]json.RawMessage
   158  	if err := json.Unmarshal(data, &fields); err != nil {
   159  		return err
   160  	}
   161  
   162  	var val json.RawMessage
   163  	val = func() json.RawMessage {
   164  		if v, ok := fields["StringField"]; ok {
   165  			return v
   166  		}
   167  		return nil
   168  	}()
   169  
   170  	if val != nil {
   171  		if err := json.Unmarshal([]byte(val), &r.StringField); err != nil {
   172  			return err
   173  		}
   174  	} else {
   175  		return fmt.Errorf("no value specified for StringField")
   176  	}
   177  	val = func() json.RawMessage {
   178  		if v, ok := fields["BoolField"]; ok {
   179  			return v
   180  		}
   181  		return nil
   182  	}()
   183  
   184  	if val != nil {
   185  		if err := json.Unmarshal([]byte(val), &r.BoolField); err != nil {
   186  			return err
   187  		}
   188  	} else {
   189  		return fmt.Errorf("no value specified for BoolField")
   190  	}
   191  	val = func() json.RawMessage {
   192  		if v, ok := fields["BytesField"]; ok {
   193  			return v
   194  		}
   195  		return nil
   196  	}()
   197  
   198  	if val != nil {
   199  		if err := json.Unmarshal([]byte(val), &r.BytesField); err != nil {
   200  			return err
   201  		}
   202  	} else {
   203  		return fmt.Errorf("no value specified for BytesField")
   204  	}
   205  	return nil
   206  }