From 8f67617e8f3761aede193fe7fe6971681aab1750 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/payloadv2.go | 30 +++++++++++++++++++++++++++--- pkg/vss/convert/payloadv2_test.go | 2 +- 3 files changed, 59 insertions(+), 4 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/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..1bf6e7e 100644 --- a/pkg/vss/convert/payloadv2_test.go +++ b/pkg/vss/convert/payloadv2_test.go @@ -122,7 +122,7 @@ var fullV2InputJSON = `{ ] } }, - "tokenId": 123, + "vehicleTokenId": 123, "make": "", "model": "", "year": 0