Skip to content

Commit

Permalink
feat: expose record and field doc (#132)
Browse files Browse the repository at this point in the history
  • Loading branch information
dstendardi authored Nov 22, 2021
1 parent 6fc3396 commit 89b6774
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
22 changes: 22 additions & 0 deletions schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,7 @@ type RecordSchema struct {

isError bool
fields []*Field
doc string
}

// NewRecordSchema creates a new record schema instance.
Expand Down Expand Up @@ -371,6 +372,11 @@ func (s *RecordSchema) Type() Type {
return Record
}

// Doc returns the documentation of a record.
func (s *RecordSchema) Doc() string {
return s.doc
}

// IsError determines is this is an error record.
func (s *RecordSchema) IsError() bool {
return s.isError
Expand Down Expand Up @@ -429,11 +435,17 @@ func (s *RecordSchema) FingerprintUsing(typ FingerprintType) ([]byte, error) {
return s.fingerprinter.FingerprintUsing(typ, s)
}

// AddDoc add documentation to the record.
func (s *RecordSchema) AddDoc(doc string) {
s.doc = doc
}

// Field is an Avro record type field.
type Field struct {
properties

name string
doc string
typ Schema
hasDef bool
def interface{}
Expand Down Expand Up @@ -483,6 +495,11 @@ func (f *Field) HasDefault() bool {
return f.hasDef
}

// AddDoc add documentation to the field.
func (f *Field) AddDoc(doc string) {
f.doc = doc
}

// Default returns the default of a field or nil.
//
// The only time a nil default is valid is for a Null Type.
Expand All @@ -494,6 +511,11 @@ func (f *Field) Default() interface{} {
return f.def
}

// Doc returns the documentation of a field.
func (f *Field) Doc() string {
return f.doc
}

// String returns the canonical form of a field.
func (f *Field) String() string {
return `{"name":"` + f.name + `","type":` + f.typ.String() + `}`
Expand Down
14 changes: 14 additions & 0 deletions schema_parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,9 @@ func parseRecord(typ Type, namespace string, m map[string]interface{}, cache *Sc
return nil, err
}

doc := resolveDoc(m)
rec.AddDoc(doc)

cache.Add(rec.FullName(), NewRefSchema(rec))

for k, v := range m {
Expand Down Expand Up @@ -248,6 +251,9 @@ func parseField(namespace string, v interface{}, cache *SchemaCache) (*Field, er
return nil, err
}

doc := resolveDoc(m)
field.AddDoc(doc)

for k, v := range m {
field.AddProp(k, v)
}
Expand Down Expand Up @@ -437,6 +443,14 @@ func resolveName(m map[string]interface{}) (string, error) {
return name, nil
}

func resolveDoc(m map[string]interface{}) string {
doc, ok := m["doc"].(string)
if !ok {
return ""
}
return doc
}

func resolveFullName(m map[string]interface{}) (string, string, error) {
name, err := resolveName(m)
if err != nil {
Expand Down

0 comments on commit 89b6774

Please sign in to comment.