Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow LexiconTypeDecoder to handle unknown types (at least from JSON) #485

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion lex/util/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func JsonDecodeValue(b []byte) (any, error) {

t, ok := lexTypesMap[tstr]
if !ok {
return nil, fmt.Errorf("%w: %q", ErrUnrecognizedType, tstr)
t = reflect.TypeOf(UnknownType{})
}

val := reflect.New(t)
Expand Down Expand Up @@ -76,6 +76,10 @@ func CborDecodeValue(b []byte) (CBOR, error) {

t, ok := lexTypesMap[tstr]
if !ok {
// It is possible to gracefully handle this case, like in JsonDecodeValue,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ScanForLinks should properly advance the reader to the next object, ill mess around with this and see if we can get it working correctly

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW, we don't necessarily have to do both in one go. Skipping CBOR for now means that firehose consumers will error out on unrecognized types. But having JSON calls not fail alone would be already an improvement.

My largest concern however is that this will trigger new code paths, shifting errors down the road. And it will uncover any bugs that are currently hidden behind a failure to unmarshal the whole message.

// but since cbg.CBORUnmarshaller takes io.Reader as an input,
// that requires a function that would correctly advance the reader
// to the end of a value (and return the bytes).
return nil, fmt.Errorf("%w: %q", ErrUnrecognizedType, tstr)
}

Expand Down Expand Up @@ -112,6 +116,12 @@ func (ltd *LexiconTypeDecoder) MarshalJSON() ([]byte, error) {
if ltd == nil || ltd.Val == nil {
return nil, fmt.Errorf("LexiconTypeDecoder MarshalJSON called on a nil")
}

if v, ok := ltd.Val.(*UnknownType); ok {
// Shortcut for passing through unrecognized values
return v.MarshalJSON()
}

v := reflect.ValueOf(ltd.Val)
t := v.Type()
sf, ok := t.Elem().FieldByName("LexiconTypeID")
Expand Down
12 changes: 12 additions & 0 deletions lex/util/decoder_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package util

import (
"encoding/json"
"testing"
)

Expand Down Expand Up @@ -37,3 +38,14 @@ func TestNewFromType(t *testing.T) {
t.Fatal("expect bogus generation to fail")
}
}

func TestMissingTypeField(t *testing.T) {
var out struct {
Field *LexiconTypeDecoder
}
const input = `{"Field":{"Some_stuff":"but $type is missing"}}`

if err := json.Unmarshal([]byte(input), &out); err != nil {
t.Fatalf("failed to unmarshal: %s", err)
}
}
16 changes: 16 additions & 0 deletions lex/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,19 @@ func CborTypeExtractReader(r io.Reader) (string, []byte, error) {

return tcheck.Type, buf.Bytes(), nil
}

type UnknownType struct {
JSON json.RawMessage
}

func (t *UnknownType) MarshalJSON() ([]byte, error) {
return t.JSON.MarshalJSON()
}

func (t *UnknownType) UnmarshalJSON(b []byte) error {
return t.JSON.UnmarshalJSON(b)
}

func (t *UnknownType) MarshalCBOR(w io.Writer) error {
return fmt.Errorf("converting unrecognized types from JSON to CBOR is not implemented")
}