From 8e5794bbbf1835c8228a696a6bf2826b3d70c155 Mon Sep 17 00:00:00 2001 From: Kevin Joiner <10265309+KevinJoiner@users.noreply.github.com> Date: Fri, 19 Apr 2024 15:49:01 -0400 Subject: [PATCH] Add dynamic conversion based on spec type --- pkg/vss/convert/benchmark_test.go | 31 +++++++++++++++++++++++++++++++ pkg/vss/convert/payloadv1_test.go | 2 +- pkg/vss/convert/payloadv2.go | 30 +++++++++++++++++++++++++++--- pkg/vss/convert/payloadv2_test.go | 6 +++--- 4 files changed, 62 insertions(+), 7 deletions(-) create mode 100644 pkg/vss/convert/benchmark_test.go diff --git a/pkg/vss/convert/benchmark_test.go b/pkg/vss/convert/benchmark_test.go new file mode 100644 index 0000000..466e05e --- /dev/null +++ b/pkg/vss/convert/benchmark_test.go @@ -0,0 +1,31 @@ +package convert_test + +import ( + "context" + "testing" + + "github.com/DIMO-Network/model-garage/pkg/vss/convert" +) + +func BenchmarkConvertFromV1DataConversion(b *testing.B) { + getter := &tokenGetter{} + inputData := []byte(fullInputJSON) + b.ResetTimer() + for i := 0; i < b.N; i++ { + _, err := convert.SignalsFromV1Payload(context.Background(), getter, inputData) + if err != nil { + b.Fatalf("error converting full input data: %v", err) + } + } +} + +func BenchmarkConvertFromV2DataConversion(b *testing.B) { + inputData := []byte(fullV2InputJSON) + b.ResetTimer() + for i := 0; i < b.N; i++ { + _, err := convert.SignalsFromV2Payload(inputData) + if err != nil { + b.Fatalf("error converting full input data: %v", err) + } + } +} diff --git a/pkg/vss/convert/payloadv1_test.go b/pkg/vss/convert/payloadv1_test.go index 3b21457..e42630c 100644 --- a/pkg/vss/convert/payloadv1_test.go +++ b/pkg/vss/convert/payloadv1_test.go @@ -21,7 +21,7 @@ func (*tokenGetter) TokenIDFromSubject(context.Context, string) (uint32, error) func TestFullFromDataConversion(t *testing.T) { t.Parallel() getter := &tokenGetter{} - actualSignals, err := convert.SignalsFromV1Payload(context.Background(), getter, []byte(fullInputJSON)) + actualSignals, err := convert.SignalsFromPayload(context.Background(), getter, []byte(fullInputJSON)) require.NoErrorf(t, err, "error converting full input data: %v", err) slices.SortFunc(expectedSignals, func(i, j vss.Signal) int { return cmp.Compare(i.Name, j.Name) diff --git a/pkg/vss/convert/payloadv2.go b/pkg/vss/convert/payloadv2.go index cde0abb..3cdb357 100644 --- a/pkg/vss/convert/payloadv2.go +++ b/pkg/vss/convert/payloadv2.go @@ -1,6 +1,7 @@ package convert import ( + "context" "errors" "fmt" "math" @@ -10,6 +11,29 @@ import ( "github.com/tidwall/gjson" ) +const ( + specV1 = "1.0" + specV2 = "2.0" +) + +// SignalsFromPayload extracts signals from a payload. +// It detects the payload version and calls the appropriate function. +func SignalsFromPayload(ctx context.Context, tokenGetter TokenIDGetter, jsonData []byte) ([]vss.Signal, error) { + specVersion := gjson.GetBytes(jsonData, "specversion").String() + switch { + case specVersion == specV1: + return SignalsFromV1Payload(ctx, tokenGetter, jsonData) + case specVersion == specV2: + return SignalsFromV2Payload(jsonData) + default: + return nil, errors.New("unsupported specversion") + } +} + +func isV1Payload(jsonData []byte) bool { + return gjson.GetBytes(jsonData, "specversion").String() == "1.0" +} + // SignalsFromV2Payload extracts signals from a V2 payload. func SignalsFromV2Payload(jsonData []byte) ([]vss.Signal, error) { var errs error @@ -49,13 +73,13 @@ func SignalsFromV2Payload(jsonData []byte) ([]vss.Signal, error) { } func tokenIDFromV2Data(jsonData []byte) (uint32, error) { - tokenID := gjson.GetBytes(jsonData, "tokenId") + tokenID := gjson.GetBytes(jsonData, "vehicleTokenID") if !tokenID.Exists() { - return 0, errors.New("tokenID field not found") + return 0, errors.New("vehicleTokenID field not found") } id, ok := tokenID.Value().(float64) if !ok { - return 0, errors.New("tokenID field is not a number") + return 0, errors.New("vehicleTokenID field is not a number") } return float64toUint32(id), nil } diff --git a/pkg/vss/convert/payloadv2_test.go b/pkg/vss/convert/payloadv2_test.go index 251917d..18ed8d6 100644 --- a/pkg/vss/convert/payloadv2_test.go +++ b/pkg/vss/convert/payloadv2_test.go @@ -13,7 +13,7 @@ const tokenID = uint32(123) func TestFullFromV2DataConversion(t *testing.T) { t.Parallel() - actualSignals, err := convert.SignalsFromV2Payload([]byte(fullV2InputJSON)) + actualSignals, err := convert.SignalsFromPayload(nil, nil, []byte(fullV2InputJSON)) require.NoErrorf(t, err, "error converting full input data: %v", err) require.Equalf(t, expectedV2Signals, actualSignals, "converted vehicle does not match expected vehicle") } @@ -21,7 +21,7 @@ func TestFullFromV2DataConversion(t *testing.T) { var fullV2InputJSON = `{ "id": "2fHbFXPWzrVActDb7WqWCfqeiYe", "source": "aftermarket/device/status", - "specversion": "1.0", + "specversion": "2.0", "subject": "0x98D78d711C0ec544F6fb5d54fcf6559CF41546a9", "time": "2024-04-18T17:20:46.436008782Z", "type": "com.dimo.device.status", @@ -122,7 +122,7 @@ var fullV2InputJSON = `{ ] } }, - "tokenId": 123, + "vehicleTokenID": 123, "make": "", "model": "", "year": 0