Documentation ¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrUnsupportedType = errors.New("unsupported avro type") ErrSchemaValueMismatch = errors.New("avro schema doesn't match supplied value") )
Functions ¶
This section is empty.
Types ¶
type Builder ¶
type Builder struct {
// contains filtered or unexported fields
}
Builder builds avro.RecordSchema instances and marshals them into JSON. Builder accepts arguments for creating fields and creates them internally (i.e. a user doesn't need to create the fields). All errors will be returned as a joined error when marshaling the schema to JSON.
Example ¶
package main import ( "fmt" "github.com/goccy/go-json" "github.com/hamba/avro/v2" ) func main() { enumSchema, err := avro.NewEnumSchema("enum_schema", "enum_namespace", []string{"val1", "val2", "val3"}) if err != nil { panic(err) } bytes, err := NewBuilder("schema_name", "schema_namespace"). AddField("int_field", avro.NewPrimitiveSchema(avro.Int, nil), avro.WithDefault(100)). AddField("enum_field", enumSchema). MarshalJSON() if err != nil { panic(err) } prettyPrint(bytes) } func prettyPrint(bytes []byte) { m := map[string]interface{}{} err := json.Unmarshal(bytes, &m) if err != nil { panic(err) } pretty, err := json.MarshalIndent(m, "", " ") if err != nil { panic(err) } fmt.Println(string(pretty)) }
Output: { "fields": [ { "default": 100, "name": "int_field", "type": "int" }, { "name": "enum_field", "type": { "name": "enum_namespace.enum_schema", "symbols": [ "val1", "val2", "val3" ], "type": "enum" } } ], "name": "schema_namespace.schema_name", "type": "record" }
func NewBuilder ¶
NewBuilder constructs a new Builder and initializes it with the given name and namespace.
func (*Builder) AddField ¶
AddField adds a new field with the given name, schema and schema options. If creating the field returns an error, the error is saved, joined with other errors (if any), and returned when marshaling to JSON.
func (*Builder) Build ¶
Build builds the underlying schema. Errors that occurred while creating fields or constructing the schema will be returned as a joined error.
func (*Builder) MarshalJSON ¶
MarshalJSON marshals the underlying schema to JSON. Errors that occurred while creating fields, constructing the schema or marshaling it will be returned as a joined error.
type Serde ¶
type Serde struct {
// contains filtered or unexported fields
}
Serde represents an Avro schema. It exposes methods for marshaling and unmarshalling data.
func SerdeForType ¶
SerdeForType uses reflection to extract an Avro schema from v. Maps are regarded as structs.
func (*Serde) Marshal ¶
Marshal returns the Avro encoding of v. Note that this function may mutate v. Limitations: - Map keys need to be of type string, - Array values need to be of type uint8 (byte).
func (*Serde) Unmarshal ¶
Unmarshal parses the Avro encoded data and stores the result in the value pointed to by v. If v is nil or not a pointer, Unmarshal returns an error. Note that arrays and maps are unmarshalled into slices and maps with untyped values (i.e. []any and map[string]any). This is a limitation of the Avro library used for encoding/decoding the payload.