Skip to content

Commit

Permalink
remove option ID
Browse files Browse the repository at this point in the history
  • Loading branch information
lovromazgon committed Aug 27, 2023
1 parent 8aaf14f commit 8591c09
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 13 deletions.
15 changes: 3 additions & 12 deletions pkg/sr/serde.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,6 @@ func GenerateFn(fn func() any) EncodingOpt {
return encodingOpt{func(t *tserde) { t.gen = fn }}
}

// ID forces Serde.Encode to use the specified schema ID.
func ID(id int) EncodingOpt {
return encodingOpt{func(opts *tserde) { opts.id = uint32(id) }}
}

// Index attaches a message index to a value. A single schema ID can be
// registered multiple times with different indices.
//
Expand Down Expand Up @@ -201,10 +196,6 @@ func (s *Serde) Register(id int, v any, opts ...EncodingOpt) {
for _, opt := range opts {
opt.apply(&t)
}
if id > 0 {
// The explicitly supplied id takes precedence over an ID EncodingOpt.
t.id = uint32(id)
}

typeof := reflect.TypeOf(v)

Expand All @@ -218,7 +209,7 @@ func (s *Serde) Register(id int, v any, opts ...EncodingOpt) {
}

// For IDs, we deeply clone any path that is changing.
dupIDs := tserdeMapClone(s.loadIDs(), int(t.id), t.index)
dupIDs := tserdeMapClone(s.loadIDs(), id, t.index)

// We defer the store because we modify the tserde below, and we
// may delete a type key.
Expand All @@ -231,7 +222,7 @@ func (s *Serde) Register(id int, v any, opts ...EncodingOpt) {
// Now we have a full path down index initialized (or, the top
// level map if there is no index). We iterate down the index
// tree to find the end node we are initializing.
k := int(t.id)
k := id
m := dupIDs
at := m[k]
depth := len(t.index)
Expand Down Expand Up @@ -261,7 +252,7 @@ func (s *Serde) Register(id int, v any, opts ...EncodingOpt) {

// Now, we initialize the end node.
t = tserde{
id: t.id,
id: uint32(id),
exists: true,
encode: t.encode,
appendEncode: t.appendEncode,
Expand Down
2 changes: 1 addition & 1 deletion pkg/sr/serde_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func TestSerde(t *testing.T) {
serde.Register(3, idx4{}, Index(0, 0, 1))
serde.Register(3, idx3{}, Index(0, 0))
serde.Register(5, oneidx{}, Index(0), GenerateFn(func() any { return &oneidx{Foo: "defoo", Bar: "debar"} }))
serde.Register(0, nil, ID(100), Index(0), EncodeFn(func(v any) ([]byte, error) {
serde.Register(100, nil, Index(0), EncodeFn(func(v any) ([]byte, error) {
return json.MarshalIndent(v, "", " ")
}))

Expand Down

0 comments on commit 8591c09

Please sign in to comment.