• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

hamba / avro / 14281844317

05 Apr 2025 12:22PM UTC coverage: 95.594% (-0.1%) from 95.7%
14281844317

Pull #514

github

web-flow
Merge 1f57f9708 into ec3a95466
Pull Request #514: draft proposal: SOE coders

117 of 131 new or added lines in 5 files covered. (89.31%)

1 existing line in 1 file now uncovered.

6032 of 6310 relevant lines covered (95.59%)

17866.37 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.0
/soe/basic.go
1
package soe
2

3
import (
4
        "bytes"
5
        "fmt"
6

7
        "github.com/hamba/avro/v2"
8
)
9

10
// Codec marshals values to/from bytes, with the Avro binary payload wrapped in
11
// an SOE frame containing the writer schema fingerprint.
12
type Codec struct {
13
        api    avro.API
14
        schema avro.Schema
15
        header []byte
16
}
17

18
// NewCodec creates a new Codec for a Schema and the default config.
19
func NewCodec(schema avro.Schema) (*Codec, error) {
14✔
20
        return NewCodecWithAPI(schema, avro.DefaultConfig)
14✔
21
}
14✔
22

23
// NewCodecWithAPI creates a new Codec for a Schema and an API.
24
func NewCodecWithAPI(schema avro.Schema, api avro.API) (*Codec, error) {
19✔
25
        // Precompute SOE header
19✔
26
        header, err := BuildHeader(schema)
19✔
27
        if err != nil {
19✔
NEW
28
                return nil, err
×
NEW
29
        }
×
30
        return &Codec{
19✔
31
                schema: schema,
19✔
32
                api:    api,
19✔
33
                header: header,
19✔
34
        }, nil
19✔
35
}
36

37
// Encode marshals a value to SOE-encoded Avro binary.
38
func (c *Codec) Encode(v any) ([]byte, error) {
13✔
39
        data, err := c.api.Marshal(c.schema, v)
13✔
40
        if err != nil {
13✔
NEW
41
                return nil, err
×
NEW
42
        }
×
43
        return append(c.header, data...), nil
13✔
44
}
45

46
// Decode unmarshals a value from SOE-encoded Avro binary, and fails if
47
// the schema fingerprint doesn't match the held schema.
48
func (c *Codec) Decode(data []byte, v any) error {
8✔
49
        fingerprint, data, err := ParseHeader(data)
8✔
50
        if err != nil {
12✔
51
                return err
4✔
52
        }
4✔
53
        expected := c.getFingerprint()
4✔
54
        if !bytes.Equal(fingerprint, expected) {
6✔
55
                return fmt.Errorf("bad fingerprint %x, expected %x", fingerprint, expected)
2✔
56
        }
2✔
57
        return c.api.Unmarshal(c.schema, data, v)
2✔
58
}
59

60
// DecodeUnverified unmarshals a value from SOE-encoded Avro binary without
61
// validating the schema fingerprint.
62
func (c *Codec) DecodeUnverified(data []byte, v any) error {
6✔
63
        _, data, err := ParseHeader(data)
6✔
64
        if err != nil {
10✔
65
                return err
4✔
66
        }
4✔
67
        return c.api.Unmarshal(c.schema, data, v)
2✔
68
}
69

70
func (c *Codec) getFingerprint() []byte {
4✔
71
        // We know the header is well-formed here, so make assumptions.
4✔
72
        return c.header[2:]
4✔
73
}
4✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc