Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type Reflector struct {
EmitAllFields bool // don't skip struct fields which have no struct tags
SkipTagFieldNames bool // don't use json/bson tag names, even if theyre present
Mapper func(reflect.Type) any
Namespace string
recordTypeCache map[string]reflect.Type
}

Expand Down Expand Up @@ -86,7 +87,11 @@ func (r *Reflector) handleRecord(t reflect.Type) *AvroSchema {
return &AvroSchema{Name: name, Type: t.Name()}
}

ret := &AvroSchema{Name: name, Type: "record"}
ret := &AvroSchema{
Name: name,
Type: "record",
Namespace: r.Namespace,
}

for i, n := 0, t.NumField(); i < n; i++ { // handle fields
f := t.Field(i)
Expand Down
43 changes: 43 additions & 0 deletions reflect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -618,3 +618,46 @@ func TestGoEmitAllFieldsGoNaming(t *testing.T) {
assert.Nil(t, err)
assert.JSONEq(t, expected, r)
}

func TestNamespace(t *testing.T) {
type Address struct {
Street string `json:"street"`
City string `json:"city"`
Country string `json:"country"`
}

type Person struct {
Name string `json:"name"`
Age int `json:"age"`
Address Address `json:"address"`
}

expected := `{
"name": "Person",
"type": "record",
"namespace": "com.example",
"fields": [
{"name": "name", "type": "string"},
{"name": "age", "type": "int"},
{"name": "address", "type": {
"name": "Address",
"type": "record",
"namespace": "com.example",
"fields": [
{"name": "street", "type": "string"},
{"name": "city", "type": "string"},
{"name": "country", "type": "string"}
]
}}
]
}`

p := Person{}
r := &Reflector{
Namespace: "com.example",
}
result, err := r.Reflect(p)

assert.Nil(t, err)
assert.JSONEq(t, expected, result)
}