From c50de24970c866e6611be8ec3572c90c2cfd2778 Mon Sep 17 00:00:00 2001 From: LukasLeung Date: Fri, 4 Feb 2022 09:09:17 -0800 Subject: [PATCH] Enable Fully Qualified Field Names --- v10/schema/fixed.go | 2 +- ...m_company_shared_conflicting_fixed_type.go | 75 ++++++++ ...com_company_team_conflicting_fixed_type.go | 71 +++++++ ...conflicting_namespace_fixed_test_record.go | 181 ++++++++++++++++++ ...g_namespace_fixed_test_record_container.go | 49 +++++ .../fixed-multiple-namespaces/fixtures.json | 14 ++ .../fixed-multiple-namespaces/generate.go | 5 + .../fixed-multiple-namespaces/schema.avsc | 24 +++ .../fixed-multiple-namespaces/schema_test.go | 18 ++ 9 files changed, 438 insertions(+), 1 deletion(-) create mode 100644 v10/test/fixed-multiple-namespaces/com_company_shared_conflicting_fixed_type.go create mode 100644 v10/test/fixed-multiple-namespaces/com_company_team_conflicting_fixed_type.go create mode 100644 v10/test/fixed-multiple-namespaces/conflicting_namespace_fixed_test_record.go create mode 100644 v10/test/fixed-multiple-namespaces/conflicting_namespace_fixed_test_record_container.go create mode 100644 v10/test/fixed-multiple-namespaces/fixtures.json create mode 100644 v10/test/fixed-multiple-namespaces/generate.go create mode 100644 v10/test/fixed-multiple-namespaces/schema.avsc create mode 100644 v10/test/fixed-multiple-namespaces/schema_test.go diff --git a/v10/schema/fixed.go b/v10/schema/fixed.go index 25815334..a46dd02d 100644 --- a/v10/schema/fixed.go +++ b/v10/schema/fixed.go @@ -35,7 +35,7 @@ func (s *FixedDefinition) Aliases() []QualifiedName { } func (s *FixedDefinition) GoType() string { - return generator.ToPublicName(s.name.Name) + return generator.ToPublicName(s.name.String()) } func (s *FixedDefinition) SizeBytes() int { diff --git a/v10/test/fixed-multiple-namespaces/com_company_shared_conflicting_fixed_type.go b/v10/test/fixed-multiple-namespaces/com_company_shared_conflicting_fixed_type.go new file mode 100644 index 00000000..2fb2e1f8 --- /dev/null +++ b/v10/test/fixed-multiple-namespaces/com_company_shared_conflicting_fixed_type.go @@ -0,0 +1,75 @@ +// Code generated by github.com/actgardner/gogen-avro/v10. DO NOT EDIT. +/* + * SOURCE: + * schema.avsc + */ +package avro + +import ( + "encoding/json" + "io" + + "github.com/actgardner/gogen-avro/v10/util" + "github.com/actgardner/gogen-avro/v10/vm/types" +) + +func writeComCompanySharedConflictingFixedType(r ComCompanySharedConflictingFixedType, w io.Writer) error { + _, err := w.Write(r[:]) + return err +} + +type ComCompanySharedConflictingFixedTypeWrapper struct { + Target *ComCompanySharedConflictingFixedType +} + +type ComCompanySharedConflictingFixedType [2]byte + +func (b *ComCompanySharedConflictingFixedType) UnmarshalJSON(data []byte) error { + var s string + if err := json.Unmarshal(data, &s); err != nil { + return err + } + codepoints := util.DecodeByteString(s) + copy((*b)[:], codepoints) + return nil +} + +func (b ComCompanySharedConflictingFixedType) MarshalJSON() ([]byte, error) { + return []byte(util.EncodeByteString(b[:])), nil +} + +func (_ ComCompanySharedConflictingFixedTypeWrapper) SetBoolean(v bool) { + panic("Unsupported operation") +} +func (_ ComCompanySharedConflictingFixedTypeWrapper) SetInt(v int32) { panic("Unsupported operation") } +func (_ ComCompanySharedConflictingFixedTypeWrapper) SetLong(v int64) { panic("Unsupported operation") } +func (_ ComCompanySharedConflictingFixedTypeWrapper) SetFloat(v float32) { + panic("Unsupported operation") +} +func (_ ComCompanySharedConflictingFixedTypeWrapper) SetDouble(v float64) { + panic("Unsupported operation") +} +func (r ComCompanySharedConflictingFixedTypeWrapper) SetBytes(v []byte) { + copy((*r.Target)[:], v) +} +func (_ ComCompanySharedConflictingFixedTypeWrapper) SetString(v string) { + panic("Unsupported operation") +} +func (_ ComCompanySharedConflictingFixedTypeWrapper) SetUnionElem(v int64) { + panic("Unsupported operation") +} +func (_ ComCompanySharedConflictingFixedTypeWrapper) Get(i int) types.Field { + panic("Unsupported operation") +} +func (_ ComCompanySharedConflictingFixedTypeWrapper) AppendMap(key string) types.Field { + panic("Unsupported operation") +} +func (_ ComCompanySharedConflictingFixedTypeWrapper) AppendArray() types.Field { + panic("Unsupported operation") +} +func (_ ComCompanySharedConflictingFixedTypeWrapper) NullField(int) { panic("Unsupported operation") } +func (_ ComCompanySharedConflictingFixedTypeWrapper) HintSize(int) { panic("Unsupported operation") } +func (_ ComCompanySharedConflictingFixedTypeWrapper) Finalize() {} +func (_ ComCompanySharedConflictingFixedTypeWrapper) SetDefault(i int) { + panic("Unsupported operation") +} diff --git a/v10/test/fixed-multiple-namespaces/com_company_team_conflicting_fixed_type.go b/v10/test/fixed-multiple-namespaces/com_company_team_conflicting_fixed_type.go new file mode 100644 index 00000000..a50849e0 --- /dev/null +++ b/v10/test/fixed-multiple-namespaces/com_company_team_conflicting_fixed_type.go @@ -0,0 +1,71 @@ +// Code generated by github.com/actgardner/gogen-avro/v10. DO NOT EDIT. +/* + * SOURCE: + * schema.avsc + */ +package avro + +import ( + "encoding/json" + "io" + + "github.com/actgardner/gogen-avro/v10/util" + "github.com/actgardner/gogen-avro/v10/vm/types" +) + +func writeComCompanyTeamConflictingFixedType(r ComCompanyTeamConflictingFixedType, w io.Writer) error { + _, err := w.Write(r[:]) + return err +} + +type ComCompanyTeamConflictingFixedTypeWrapper struct { + Target *ComCompanyTeamConflictingFixedType +} + +type ComCompanyTeamConflictingFixedType [1]byte + +func (b *ComCompanyTeamConflictingFixedType) UnmarshalJSON(data []byte) error { + var s string + if err := json.Unmarshal(data, &s); err != nil { + return err + } + codepoints := util.DecodeByteString(s) + copy((*b)[:], codepoints) + return nil +} + +func (b ComCompanyTeamConflictingFixedType) MarshalJSON() ([]byte, error) { + return []byte(util.EncodeByteString(b[:])), nil +} + +func (_ ComCompanyTeamConflictingFixedTypeWrapper) SetBoolean(v bool) { panic("Unsupported operation") } +func (_ ComCompanyTeamConflictingFixedTypeWrapper) SetInt(v int32) { panic("Unsupported operation") } +func (_ ComCompanyTeamConflictingFixedTypeWrapper) SetLong(v int64) { panic("Unsupported operation") } +func (_ ComCompanyTeamConflictingFixedTypeWrapper) SetFloat(v float32) { + panic("Unsupported operation") +} +func (_ ComCompanyTeamConflictingFixedTypeWrapper) SetDouble(v float64) { + panic("Unsupported operation") +} +func (r ComCompanyTeamConflictingFixedTypeWrapper) SetBytes(v []byte) { + copy((*r.Target)[:], v) +} +func (_ ComCompanyTeamConflictingFixedTypeWrapper) SetString(v string) { + panic("Unsupported operation") +} +func (_ ComCompanyTeamConflictingFixedTypeWrapper) SetUnionElem(v int64) { + panic("Unsupported operation") +} +func (_ ComCompanyTeamConflictingFixedTypeWrapper) Get(i int) types.Field { + panic("Unsupported operation") +} +func (_ ComCompanyTeamConflictingFixedTypeWrapper) AppendMap(key string) types.Field { + panic("Unsupported operation") +} +func (_ ComCompanyTeamConflictingFixedTypeWrapper) AppendArray() types.Field { + panic("Unsupported operation") +} +func (_ ComCompanyTeamConflictingFixedTypeWrapper) NullField(int) { panic("Unsupported operation") } +func (_ ComCompanyTeamConflictingFixedTypeWrapper) HintSize(int) { panic("Unsupported operation") } +func (_ ComCompanyTeamConflictingFixedTypeWrapper) Finalize() {} +func (_ ComCompanyTeamConflictingFixedTypeWrapper) SetDefault(i int) { panic("Unsupported operation") } diff --git a/v10/test/fixed-multiple-namespaces/conflicting_namespace_fixed_test_record.go b/v10/test/fixed-multiple-namespaces/conflicting_namespace_fixed_test_record.go new file mode 100644 index 00000000..cfc66863 --- /dev/null +++ b/v10/test/fixed-multiple-namespaces/conflicting_namespace_fixed_test_record.go @@ -0,0 +1,181 @@ +// Code generated by github.com/actgardner/gogen-avro/v10. DO NOT EDIT. +/* + * SOURCE: + * schema.avsc + */ +package avro + +import ( + "encoding/json" + "fmt" + "io" + + "github.com/actgardner/gogen-avro/v10/compiler" + "github.com/actgardner/gogen-avro/v10/vm" + "github.com/actgardner/gogen-avro/v10/vm/types" +) + +var _ = fmt.Printf + +type ConflictingNamespaceFixedTestRecord struct { + FixedFieldOne ComCompanyTeamConflictingFixedType `json:"fixedFieldOne"` + + FixedFieldTwo ComCompanySharedConflictingFixedType `json:"fixedFieldTwo"` +} + +const ConflictingNamespaceFixedTestRecordAvroCRC64Fingerprint = "\xac\xa49=o\x88\x9d\xde" + +func NewConflictingNamespaceFixedTestRecord() ConflictingNamespaceFixedTestRecord { + r := ConflictingNamespaceFixedTestRecord{} + return r +} + +func DeserializeConflictingNamespaceFixedTestRecord(r io.Reader) (ConflictingNamespaceFixedTestRecord, error) { + t := NewConflictingNamespaceFixedTestRecord() + deser, err := compiler.CompileSchemaBytes([]byte(t.Schema()), []byte(t.Schema())) + if err != nil { + return t, err + } + + err = vm.Eval(r, deser, &t) + return t, err +} + +func DeserializeConflictingNamespaceFixedTestRecordFromSchema(r io.Reader, schema string) (ConflictingNamespaceFixedTestRecord, error) { + t := NewConflictingNamespaceFixedTestRecord() + + deser, err := compiler.CompileSchemaBytes([]byte(schema), []byte(t.Schema())) + if err != nil { + return t, err + } + + err = vm.Eval(r, deser, &t) + return t, err +} + +func writeConflictingNamespaceFixedTestRecord(r ConflictingNamespaceFixedTestRecord, w io.Writer) error { + var err error + err = writeComCompanyTeamConflictingFixedType(r.FixedFieldOne, w) + if err != nil { + return err + } + err = writeComCompanySharedConflictingFixedType(r.FixedFieldTwo, w) + if err != nil { + return err + } + return err +} + +func (r ConflictingNamespaceFixedTestRecord) Serialize(w io.Writer) error { + return writeConflictingNamespaceFixedTestRecord(r, w) +} + +func (r ConflictingNamespaceFixedTestRecord) Schema() string { + return "{\"fields\":[{\"name\":\"fixedFieldOne\",\"type\":{\"name\":\"ConflictingFixedType\",\"namespace\":\"com.company.team\",\"size\":1,\"type\":\"fixed\"}},{\"name\":\"fixedFieldTwo\",\"type\":{\"name\":\"ConflictingFixedType\",\"namespace\":\"com.company.shared\",\"size\":2,\"type\":\"fixed\"}}],\"name\":\"ConflictingNamespaceFixedTestRecord\",\"type\":\"record\"}" +} + +func (r ConflictingNamespaceFixedTestRecord) SchemaName() string { + return "ConflictingNamespaceFixedTestRecord" +} + +func (_ ConflictingNamespaceFixedTestRecord) SetBoolean(v bool) { panic("Unsupported operation") } +func (_ ConflictingNamespaceFixedTestRecord) SetInt(v int32) { panic("Unsupported operation") } +func (_ ConflictingNamespaceFixedTestRecord) SetLong(v int64) { panic("Unsupported operation") } +func (_ ConflictingNamespaceFixedTestRecord) SetFloat(v float32) { panic("Unsupported operation") } +func (_ ConflictingNamespaceFixedTestRecord) SetDouble(v float64) { panic("Unsupported operation") } +func (_ ConflictingNamespaceFixedTestRecord) SetBytes(v []byte) { panic("Unsupported operation") } +func (_ ConflictingNamespaceFixedTestRecord) SetString(v string) { panic("Unsupported operation") } +func (_ ConflictingNamespaceFixedTestRecord) SetUnionElem(v int64) { panic("Unsupported operation") } + +func (r *ConflictingNamespaceFixedTestRecord) Get(i int) types.Field { + switch i { + case 0: + w := ComCompanyTeamConflictingFixedTypeWrapper{Target: &r.FixedFieldOne} + + return w + + case 1: + w := ComCompanySharedConflictingFixedTypeWrapper{Target: &r.FixedFieldTwo} + + return w + + } + panic("Unknown field index") +} + +func (r *ConflictingNamespaceFixedTestRecord) SetDefault(i int) { + switch i { + } + panic("Unknown field index") +} + +func (r *ConflictingNamespaceFixedTestRecord) NullField(i int) { + switch i { + } + panic("Not a nullable field index") +} + +func (_ ConflictingNamespaceFixedTestRecord) AppendMap(key string) types.Field { + panic("Unsupported operation") +} +func (_ ConflictingNamespaceFixedTestRecord) AppendArray() types.Field { + panic("Unsupported operation") +} +func (_ ConflictingNamespaceFixedTestRecord) HintSize(int) { panic("Unsupported operation") } +func (_ ConflictingNamespaceFixedTestRecord) Finalize() {} + +func (_ ConflictingNamespaceFixedTestRecord) AvroCRC64Fingerprint() []byte { + return []byte(ConflictingNamespaceFixedTestRecordAvroCRC64Fingerprint) +} + +func (r ConflictingNamespaceFixedTestRecord) MarshalJSON() ([]byte, error) { + var err error + output := make(map[string]json.RawMessage) + output["fixedFieldOne"], err = json.Marshal(r.FixedFieldOne) + if err != nil { + return nil, err + } + output["fixedFieldTwo"], err = json.Marshal(r.FixedFieldTwo) + if err != nil { + return nil, err + } + return json.Marshal(output) +} + +func (r *ConflictingNamespaceFixedTestRecord) UnmarshalJSON(data []byte) error { + var fields map[string]json.RawMessage + if err := json.Unmarshal(data, &fields); err != nil { + return err + } + + var val json.RawMessage + val = func() json.RawMessage { + if v, ok := fields["fixedFieldOne"]; ok { + return v + } + return nil + }() + + if val != nil { + if err := json.Unmarshal([]byte(val), &r.FixedFieldOne); err != nil { + return err + } + } else { + return fmt.Errorf("no value specified for fixedFieldOne") + } + val = func() json.RawMessage { + if v, ok := fields["fixedFieldTwo"]; ok { + return v + } + return nil + }() + + if val != nil { + if err := json.Unmarshal([]byte(val), &r.FixedFieldTwo); err != nil { + return err + } + } else { + return fmt.Errorf("no value specified for fixedFieldTwo") + } + return nil +} diff --git a/v10/test/fixed-multiple-namespaces/conflicting_namespace_fixed_test_record_container.go b/v10/test/fixed-multiple-namespaces/conflicting_namespace_fixed_test_record_container.go new file mode 100644 index 00000000..fbd76ea3 --- /dev/null +++ b/v10/test/fixed-multiple-namespaces/conflicting_namespace_fixed_test_record_container.go @@ -0,0 +1,49 @@ +// Code generated by github.com/actgardner/gogen-avro/v10. DO NOT EDIT. +/* + * SOURCE: + * schema.avsc + */ +package avro + +import ( + "io" + + "github.com/actgardner/gogen-avro/v10/compiler" + "github.com/actgardner/gogen-avro/v10/container" + "github.com/actgardner/gogen-avro/v10/vm" +) + +func NewConflictingNamespaceFixedTestRecordWriter(writer io.Writer, codec container.Codec, recordsPerBlock int64) (*container.Writer, error) { + str := NewConflictingNamespaceFixedTestRecord() + return container.NewWriter(writer, codec, recordsPerBlock, str.Schema()) +} + +// container reader +type ConflictingNamespaceFixedTestRecordReader struct { + r io.Reader + p *vm.Program +} + +func NewConflictingNamespaceFixedTestRecordReader(r io.Reader) (*ConflictingNamespaceFixedTestRecordReader, error) { + containerReader, err := container.NewReader(r) + if err != nil { + return nil, err + } + + t := NewConflictingNamespaceFixedTestRecord() + deser, err := compiler.CompileSchemaBytes([]byte(containerReader.AvroContainerSchema()), []byte(t.Schema())) + if err != nil { + return nil, err + } + + return &ConflictingNamespaceFixedTestRecordReader{ + r: containerReader, + p: deser, + }, nil +} + +func (r ConflictingNamespaceFixedTestRecordReader) Read() (ConflictingNamespaceFixedTestRecord, error) { + t := NewConflictingNamespaceFixedTestRecord() + err := vm.Eval(r.r, r.p, &t) + return t, err +} diff --git a/v10/test/fixed-multiple-namespaces/fixtures.json b/v10/test/fixed-multiple-namespaces/fixtures.json new file mode 100644 index 00000000..8c20a0a0 --- /dev/null +++ b/v10/test/fixed-multiple-namespaces/fixtures.json @@ -0,0 +1,14 @@ +[ + { + "fixedFieldOne": "\u0000", + "fixedFieldTwo": "\u00ff\u00ff" + }, + { + "fixedFieldOne": "\u00ff", + "fixedFieldTwo": "\u0000\u0000" + }, + { + "fixedFieldOne": "\u0000", + "fixedFieldTwo": "\u00ff\u00ff" + } +] diff --git a/v10/test/fixed-multiple-namespaces/generate.go b/v10/test/fixed-multiple-namespaces/generate.go new file mode 100644 index 00000000..42f1a8bc --- /dev/null +++ b/v10/test/fixed-multiple-namespaces/generate.go @@ -0,0 +1,5 @@ +package avro + +//go:generate $GOPATH/bin/gogen-avro -namespaced-names=full -containers . schema.avsc +//go:generate mkdir -p evolution +//go:generate $GOPATH/bin/gogen-avro -namespaced-names=full -containers evolution evolution.avsc diff --git a/v10/test/fixed-multiple-namespaces/schema.avsc b/v10/test/fixed-multiple-namespaces/schema.avsc new file mode 100644 index 00000000..a6215854 --- /dev/null +++ b/v10/test/fixed-multiple-namespaces/schema.avsc @@ -0,0 +1,24 @@ +{ + "type": "record", + "name": "ConflictingNamespaceFixedTestRecord", + "fields": [ + { + "name": "fixedFieldOne", + "type": { + "type": "fixed", + "name": "ConflictingFixedType", + "namespace": "com.company.team", + "size": 1 + } + }, + { + "name": "fixedFieldTwo", + "type": { + "type": "fixed", + "name": "ConflictingFixedType", + "namespace": "com.company.shared", + "size": 2 + } + } + ] +} diff --git a/v10/test/fixed-multiple-namespaces/schema_test.go b/v10/test/fixed-multiple-namespaces/schema_test.go new file mode 100644 index 00000000..a955e655 --- /dev/null +++ b/v10/test/fixed-multiple-namespaces/schema_test.go @@ -0,0 +1,18 @@ +package avro + +import ( + "io" + "testing" + + "github.com/actgardner/gogen-avro/v10/container" + "github.com/actgardner/gogen-avro/v10/test" +) + +func TestRoundTrip(t *testing.T) { + test.RoundTripExactBytes(t, + func() container.AvroRecord { return &ConflictingNamespaceFixedTestRecord{} }, + func(r io.Reader) (container.AvroRecord, error) { + record, err := DeserializeConflictingNamespaceFixedTestRecord(r) + return &record, err + }) +}