Skip to content

Commit

Permalink
Update status schema version handling
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinJoiner committed May 20, 2024
1 parent 457a138 commit 0d7a135
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 20 deletions.
45 changes: 45 additions & 0 deletions pkg/vss/convert/convert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package convert

import (
"context"
"strings"

"github.com/DIMO-Network/model-garage/pkg/vss"
"github.com/tidwall/gjson"
)

const (
// StatusV1 is the version string for payloads with the version 1.0 schema.
StatusV1 = "1.0"
// StatusV1Converted is the version string for payloads that have been converted to the 1.0 schema.
StatusV1Converted = "1.1"
// StatusV2 is the version string for payloads with the version 2.0 schema.
StatusV2 = "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) {
version := GetSchemaVersion(jsonData)
switch {
case version == StatusV1 || version == StatusV1Converted:
return SignalsFromV1Payload(ctx, tokenGetter, jsonData)
case version == StatusV2:
return SignalsFromV2Payload(jsonData)
default:
return nil, VersionError{Version: version}
}
}

// GetSchemaVersion returns the version string of the schema used in the payload.
func GetSchemaVersion(jsonData []byte) string {
dataSchema := gjson.GetBytes(jsonData, "dataschema")
if dataSchema.Exists() {
// get version string at the end of the URI
// Ex. dimo.zone.status/v1.1
schemaString := dataSchema.String()
version := schemaString[strings.LastIndex(schemaString, "/")+1:]
return strings.TrimPrefix(version, "v")
}
return gjson.GetBytes(jsonData, "specversion").String()
}
20 changes: 0 additions & 20 deletions pkg/vss/convert/payloadv2.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package convert

import (
"context"
"errors"
"fmt"
"math"
Expand All @@ -11,25 +10,6 @@ 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, VersionError{Version: specVersion}
}
}

// SignalsFromV2Payload extracts signals from a V2 payload.
func SignalsFromV2Payload(jsonData []byte) ([]vss.Signal, error) {
var errs error
Expand Down

0 comments on commit 0d7a135

Please sign in to comment.