Skip to content

Commit

Permalink
Add dynamic conversion based on spec type
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinJoiner committed Apr 19, 2024
1 parent a1dd442 commit 8e5794b
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 7 deletions.
31 changes: 31 additions & 0 deletions pkg/vss/convert/benchmark_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
2 changes: 1 addition & 1 deletion pkg/vss/convert/payloadv1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
30 changes: 27 additions & 3 deletions pkg/vss/convert/payloadv2.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package convert

import (
"context"
"errors"
"fmt"
"math"
Expand All @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/vss/convert/payloadv2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ 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")
}

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",
Expand Down Expand Up @@ -122,7 +122,7 @@ var fullV2InputJSON = `{
]
}
},
"tokenId": 123,
"vehicleTokenID": 123,
"make": "",
"model": "",
"year": 0
Expand Down

0 comments on commit 8e5794b

Please sign in to comment.