Skip to content

Commit

Permalink
Export data extraction functions
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinJoiner committed May 20, 2024
1 parent 0d7a135 commit 8085430
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 15 deletions.
2 changes: 1 addition & 1 deletion pkg/vss/convert/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ type VersionError struct {

// Error returns the error message.
func (e VersionError) Error() string {
return fmt.Sprintf("unsupported specversion: %s", e.Version)
return fmt.Sprintf("unsupported verision: %s", e.Version)
}

// FieldNotFoundError is an error for missing fields.
Expand Down
15 changes: 9 additions & 6 deletions pkg/vss/convert/payloadv1.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ type TokenIDGetter interface {

// SignalsFromV1Payload gets a slice signals from a v1 payload.
func SignalsFromV1Payload(ctx context.Context, tokenGetter TokenIDGetter, jsonData []byte) ([]vss.Signal, error) {
ts, err := timestampFromV1Data(jsonData)
ts, err := TimestampFromV1Data(jsonData)
if err != nil {
return nil, fmt.Errorf("error getting timestamp: %w", err)
}
sub, err := subjectFromV1Data(jsonData)
sub, err := SubjectFromV1Data(jsonData)
if err != nil {
return nil, fmt.Errorf("error getting subject: %w", err)
}
Expand All @@ -31,7 +31,7 @@ func SignalsFromV1Payload(ctx context.Context, tokenGetter TokenIDGetter, jsonDa
return nil, fmt.Errorf("error getting tokenID from subject: %w", err)
}

source, err := sourceFromV1Data(jsonData)
source, err := SourceFromV1Data(jsonData)
baseSignal := vss.Signal{
TokenID: tokenID,
Timestamp: ts,
Expand All @@ -44,7 +44,8 @@ func SignalsFromV1Payload(ctx context.Context, tokenGetter TokenIDGetter, jsonDa
return sigs, nil
}

func subjectFromV1Data(jsonData []byte) (string, error) {
// SubjectFromV1Data gets a subject from a v1 payload.
func SubjectFromV1Data(jsonData []byte) (string, error) {
result := gjson.GetBytes(jsonData, "subject")
if !result.Exists() {
return "", FieldNotFoundError{Field: "subject", Lookup: "subject"}
Expand All @@ -56,7 +57,8 @@ func subjectFromV1Data(jsonData []byte) (string, error) {
return sub, nil
}

func timestampFromV1Data(jsonData []byte) (time.Time, error) {
// TimestampFromV1Data gets a timestamp from a v1 payload.
func TimestampFromV1Data(jsonData []byte) (time.Time, error) {
result := gjson.GetBytes(jsonData, "time")
if !result.Exists() {
return time.Time{}, FieldNotFoundError{Field: "timestamp", Lookup: "time"}
Expand All @@ -77,7 +79,8 @@ func timestampFromV1Data(jsonData []byte) (time.Time, error) {
return ts, nil
}

func sourceFromV1Data(jsonData []byte) (string, error) {
// SourceFromV1Data gets a source field from a v1 payload.
func SourceFromV1Data(jsonData []byte) (string, error) {
result := gjson.GetBytes(jsonData, "source")
if !result.Exists() {
return "", FieldNotFoundError{Field: "source", Lookup: "source"}
Expand Down
20 changes: 12 additions & 8 deletions pkg/vss/convert/payloadv2.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ func SignalsFromV2Payload(jsonData []byte) ([]vss.Signal, error) {
if !signals.IsArray() {
return nil, errors.New("signals field is not an array")
}
tokenID, err := tokenIDFromV2Data(jsonData)
tokenID, err := TokenIDFromV2Data(jsonData)
if err != nil {
return nil, fmt.Errorf("error getting tokenID: %w", err)
}
source, err := sourceFromV2Data(jsonData)
source, err := SourceFromV2Data(jsonData)
if err != nil {
return nil, fmt.Errorf("error getting source: %w", err)
}
Expand All @@ -35,12 +35,12 @@ func SignalsFromV2Payload(jsonData []byte) ([]vss.Signal, error) {
Source: source,
}
for _, sigData := range signals.Array() {
originalName, err := signalNameFromV2Data(sigData)
originalName, err := NameFromV2Signal(sigData)
if err != nil {
errs = errors.Join(errs, err)
continue
}
ts, err := timestampFromV2Data(sigData)
ts, err := TimestampFromV2Signal(sigData)
if err != nil {
err = fmt.Errorf("error for '%s': %w", originalName, err)
errs = errors.Join(errs, err)
Expand All @@ -57,7 +57,8 @@ func SignalsFromV2Payload(jsonData []byte) ([]vss.Signal, error) {
return retSignals, errs
}

func tokenIDFromV2Data(jsonData []byte) (uint32, error) {
// TokenIDFromV2Data gets a tokenID from a V2 payload.
func TokenIDFromV2Data(jsonData []byte) (uint32, error) {
lookupKey := "vehicleTokenId"
tokenID := gjson.GetBytes(jsonData, lookupKey)
if !tokenID.Exists() {
Expand All @@ -70,7 +71,8 @@ func tokenIDFromV2Data(jsonData []byte) (uint32, error) {
return float64toUint32(id), nil
}

func timestampFromV2Data(sigResult gjson.Result) (time.Time, error) {
// TimestampFromV2Signal gets a timestamp from a V2 signal.
func TimestampFromV2Signal(sigResult gjson.Result) (time.Time, error) {
lookupKey := "timestamp"
timestamp := sigResult.Get(lookupKey)
if !timestamp.Exists() {
Expand All @@ -79,7 +81,8 @@ func timestampFromV2Data(sigResult gjson.Result) (time.Time, error) {
return time.UnixMilli(int64(timestamp.Uint())).UTC(), nil
}

func signalNameFromV2Data(sigResult gjson.Result) (string, error) {
// NameFromV2Signal gets a name from a V2 signal.
func NameFromV2Signal(sigResult gjson.Result) (string, error) {
lookupKey := "name"
signalName := sigResult.Get(lookupKey)
if !signalName.Exists() {
Expand All @@ -88,7 +91,8 @@ func signalNameFromV2Data(sigResult gjson.Result) (string, error) {
return signalName.String(), nil
}

func sourceFromV2Data(jsonData []byte) (string, error) {
// SourceFromV2Data gets a source from a V2 payload.
func SourceFromV2Data(jsonData []byte) (string, error) {
lookupKey := "source"
source := gjson.GetBytes(jsonData, lookupKey)
if !source.Exists() {
Expand Down

0 comments on commit 8085430

Please sign in to comment.