Skip to content

Commit

Permalink
fix: allow multi-line doc, and custom fields (#411)
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianiacobghiula committed Jun 21, 2024
1 parent a76983d commit e2e849d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
29 changes: 25 additions & 4 deletions schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 377,13 @@ func (p properties) marshalPropertiesToJSON(buf *bytes.Buffer) error {
if err != nil {
return err
}
buf.WriteString(`,"` k `":`)
kk, err := jsoniter.Marshal(k)
if err != nil {
return err
}
buf.WriteString(`,`)
buf.Write(kk)
buf.WriteString(`:`)
buf.Write(vv)
}
return nil
Expand Down Expand Up @@ -626,7 632,12 @@ func (s *RecordSchema) MarshalJSON() ([]byte, error) {
buf.Write(aliasesJSON)
}
if s.doc != "" {
buf.WriteString(`,"doc":"` s.doc `"`)
docJSON, err := jsoniter.Marshal(s.doc)
if err != nil {
return nil, err
}
buf.WriteString(`,"doc":`)
buf.Write(docJSON)
}
if s.isError {
buf.WriteString(`,"type":"error"`)
Expand Down Expand Up @@ -818,7 829,12 @@ func (f *Field) MarshalJSON() ([]byte, error) {
buf.Write(aliasesJSON)
}
if f.doc != "" {
buf.WriteString(`,"doc":"` f.doc `"`)
docJSON, err := jsoniter.Marshal(f.doc)
if err != nil {
return nil, err
}
buf.WriteString(`,"doc":`)
buf.Write(docJSON)
}
typeJSON, err := jsoniter.Marshal(f.typ)
if err != nil {
Expand Down Expand Up @@ -983,7 999,12 @@ func (s *EnumSchema) MarshalJSON() ([]byte, error) {
buf.Write(aliasesJSON)
}
if s.doc != "" {
buf.WriteString(`,"doc":"` s.doc `"`)
docJSON, err := jsoniter.Marshal(s.doc)
if err != nil {
return nil, err
}
buf.WriteString(`,"doc":`)
buf.Write(docJSON)
}
buf.WriteString(`,"type":"enum"`)
symbolsJSON, err := jsoniter.Marshal(s.symbols)
Expand Down
8 changes: 8 additions & 0 deletions schema_json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 151,10 @@ func TestSchema_JSON(t *testing.T) {
input: `{"fields":[], "type":"record", "name":"foo", "doc":"Useful info"}`,
json: `{"name":"foo","doc":"Useful info","type":"record","fields":[]}`,
},
{
input: `{"fields":[], "type":"record", "name":"foo", "doc":"Useful info\n on multiple \n lines"}`,
json: `{"name":"foo","doc":"Useful info\n on multiple \n lines","type":"record","fields":[]}`,
},
{
input: `{"fields":[], "type":"record", "name":"foo", "aliases":["foo","bar"]}`,
json: `{"name":"foo","aliases":["foo","bar"],"type":"record","fields":[]}`,
Expand All @@ -163,6 167,10 @@ func TestSchema_JSON(t *testing.T) {
input: `{"fields":[], "property-foo": "value-bar", "type":"record", "name":"foo", "doc":"foo", "aliases":["foo","bar"]}`,
json: `{"name":"foo","aliases":["foo","bar"],"doc":"foo","type":"record","fields":[],"property-foo":"value-bar"}`,
},
{
input: `{"fields":[], "property\"foo": "value-bar", "type":"record", "name":"foo", "doc":"foo", "aliases":["foo","bar"]}`,
json: `{"name":"foo","aliases":["foo","bar"],"doc":"foo","type":"record","fields":[],"property\"foo":"value-bar"}`,
},
{
input: `{"fields":[{"type":{"type":"boolean"}, "name":"f1"}], "type":"record", "name":"foo"}`,
json: `{"name":"foo","type":"record","fields":[{"name":"f1","type":"boolean"}]}`,
Expand Down

0 comments on commit e2e849d

Please sign in to comment.