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

hamba / avro / 14201222126

01 Apr 2025 04:41PM UTC coverage: 95.354% (-0.3%) from 95.7%
14201222126

Pull #514

github

web-flow
Merge e107d902e into f6ebd9f2c
Pull Request #514: draft proposal: SOE coders

79 of 105 new or added lines in 4 files covered. (75.24%)

5932 of 6221 relevant lines covered (95.35%)

36280.29 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) {
10✔
20
        return NewCodecWithAPI(schema, avro.DefaultConfig)
10✔
21
}
10✔
22

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

37
// Encode marshals a value to SOE-encoded Avro binary.
38
func (c *Codec) Encode(v any) ([]byte, error) {
8✔
39
        data, err := c.api.Marshal(c.schema, v)
8✔
40
        if err != nil {
8✔
NEW
41
                return nil, err
×
NEW
42
        }
×
43
        return append(c.header, data...), nil
8✔
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 {
16✔
49
        fingerprint, data, err := ParseHeader(data)
16✔
50
        if err != nil {
24✔
51
                return err
8✔
52
        }
8✔
53
        expected := c.getFingerprint()
8✔
54
        if !bytes.Equal(fingerprint, expected) {
12✔
55
                return fmt.Errorf("bad fingerprint %x, expected %x", fingerprint, expected)
4✔
56
        }
4✔
57
        return c.api.Unmarshal(c.schema, data, v)
4✔
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 {
12✔
63
        _, data, err := ParseHeader(data)
12✔
64
        if err != nil {
20✔
65
                return err
8✔
66
        }
8✔
67
        return c.api.Unmarshal(c.schema, data, v)
4✔
68
}
69

70
func (c *Codec) getFingerprint() []byte {
8✔
71
        // We know the header is well-formed here, so make assumptions.
8✔
72
        return c.header[2:]
8✔
73
}
8✔
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