Skip to content

Commit

Permalink
Update JSON name creation
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinJoiner committed May 24, 2024
1 parent 2175803 commit 670922e
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 46 deletions.
53 changes: 41 additions & 12 deletions pkg/schema/signal.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package schema
import (
"regexp"
"strings"
"unicode"
)

const (
Expand Down Expand Up @@ -109,7 +110,7 @@ func NewSignalInfo(record []string) *SignalInfo {
sig.BaseGQLType = gqlTypeFromVSPEC(baseType)
}
sig.GOName = goName(sig.Name)
sig.JSONName = JSONName(sig.Name)
sig.JSONName = jsonName(sig.Name)

return sig
}
Expand Down Expand Up @@ -146,23 +147,51 @@ func (s *SignalInfo) GQLType() string {
return s.BaseGQLType
}

// goName returns the golang formated name of the signal.
// Removes the root Prefix and nonAlphaNumeric characters from the name and capitalizes the first letter.
func goName(name string) string {
return nonAlphaNum.ReplaceAllString(removePrefix(name), "")
firstComponent, rest := splitAndSantizeName(name)
var nameBuilder strings.Builder
_, _ = nameBuilder.WriteRune(unicode.ToUpper(rune(firstComponent[0])))
_, _ = nameBuilder.WriteString(firstComponent[1:])
_, _ = nameBuilder.WriteString(rest)
return nameBuilder.String()
}

// JSONName returns the json name of the signal.
func JSONName(name string) string {
n := goName(name)
// lowercase the first letter
return strings.ToLower(n[:1]) + n[1:]
// jsonName returns the JSON formated name of the signal.
// Removes the root Prefix and nonAlphaNumeric characters from the name and lowercases the first word.
func jsonName(name string) string {
firstComponent, rest := splitAndSantizeName(name)
if firstComponent == "" {
return ""
}

var nameBuilder strings.Builder
for i, r := range firstComponent {
if i == 0 || unicode.IsUpper(r) {
// Lowercase first and any initial consecutive uppercase letters
_, _ = nameBuilder.WriteRune(unicode.ToLower(r))
continue
}
// write the remainging characters of the first component
_, _ = nameBuilder.WriteString(firstComponent[i:])
break
}
_, _ = nameBuilder.WriteString(rest)
return nameBuilder.String()
}

func removePrefix(name string) string {
idx := strings.IndexByte(name, '.')
if idx != -1 {
return name[idx+1:]
// splitAndSantizeName removes the root branch prefix from the name and returns the first component separate from rest of the name with nonAlphaNumeric characters removed.
func splitAndSantizeName(name string) (string, string) {
splitName := strings.Split(name, ".")

if len(splitName) == 1 {
return nonAlphaNum.ReplaceAllString(splitName[0], ""), ""
}
return name
// remove branch prefix if it exists i.e. Vehcile.Speed -> Speed
splitName = splitName[1:]

return nonAlphaNum.ReplaceAllString(splitName[0], ""), nonAlphaNum.ReplaceAllString(strings.Join(splitName[1:], ""), "")
}

// goTypeFromVSPEC converts vspec type to golang types.
Expand Down
48 changes: 48 additions & 0 deletions pkg/schema/signal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package schema

import "testing"

func TestJSONName(t *testing.T) {
tests := []struct {
input string
expected string
}{
{
input: "Vehicle.CurrentLocation.Altitude",
expected: "currentLocationAltitude",
},
{
input: "Vechicle.DIMO.Source",
expected: "dimoSource",
},
{
input: "Vehicle.DIMO.WPAState",
expected: "dimoWPAState",
},
{
input: "Vehicle.DIMO.Aftemarket.HDOP",
expected: "dimoAftemarketHDOP",
},
{
input: "Vehicle.DIMO.Aftemarket.WPAState",
expected: "dimoAftemarketWPAState",
},
{
input: "Vehicle.Powertrain.CombustionEngine.ECT",

Check failure on line 31 in pkg/schema/signal_test.go

View workflow job for this annotation

GitHub Actions / lint

`ECT` is a misspelling of `ETC` (misspell)
expected: "powertrainCombustionEngineECT",
},
{
input: "Vehicle.Powertrain.CombustionEngine.Tps",
expected: "powertrainCombustionEngineTps",
},
}

for _, test := range tests {
t.Run(test.input, func(t *testing.T) {
result := jsonName(test.input)
if result != test.expected {
t.Errorf("Unexpected result. Expected: %s, Got: %s", test.expected, result)
}
})
}
}
8 changes: 4 additions & 4 deletions pkg/vss/convert/payloadv1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ var (
{TokenID: 123, Timestamp: ts, Name: "vehicleIdentificationYear", ValueNumber: 2020, Source: "dimo/integration/123"},
{TokenID: 123, Timestamp: ts, Name: "exteriorAirTemperature", ValueNumber: 25, Source: "dimo/integration/123"},
{TokenID: 123, Timestamp: ts, Name: "lowVoltageBatteryCurrentVoltage", ValueNumber: 12.5, Source: "dimo/integration/123"},
{TokenID: 123, Timestamp: ts, Name: "oBDBarometricPressure", ValueNumber: 1013.25, Source: "dimo/integration/123"},
{TokenID: 123, Timestamp: ts, Name: "oBDEngineLoad", ValueNumber: 75, Source: "dimo/integration/123"},
{TokenID: 123, Timestamp: ts, Name: "oBDIntakeTemp", ValueNumber: 30, Source: "dimo/integration/123"},
{TokenID: 123, Timestamp: ts, Name: "oBDRunTime", ValueNumber: 1200, Source: "dimo/integration/123"},
{TokenID: 123, Timestamp: ts, Name: "obdBarometricPressure", ValueNumber: 1013.25, Source: "dimo/integration/123"},
{TokenID: 123, Timestamp: ts, Name: "obdEngineLoad", ValueNumber: 75, Source: "dimo/integration/123"},
{TokenID: 123, Timestamp: ts, Name: "obdIntakeTemp", ValueNumber: 30, Source: "dimo/integration/123"},
{TokenID: 123, Timestamp: ts, Name: "obdRunTime", ValueNumber: 1200, Source: "dimo/integration/123"},
}
)
12 changes: 6 additions & 6 deletions pkg/vss/convert/payloadv2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,16 +131,16 @@ var fullV2InputJSON = `{
var expectedV2Signals = []vss.Signal{
{TokenID: tokenID, Timestamp: time.Date(2024, time.April, 18, 17, 20, 26, 633000000, time.UTC), Name: "powertrainCombustionEngineECT", ValueNumber: 107, ValueString: "", Source: "dimo/integration/123"},
{TokenID: tokenID, Timestamp: time.Date(2024, time.April, 18, 17, 20, 27, 173000000, time.UTC), Name: "powertrainCombustionEngineMAF", ValueNumber: 475.79, ValueString: "", Source: "dimo/integration/123"},
{TokenID: tokenID, Timestamp: time.Date(2024, time.April, 18, 17, 20, 29, 314000000, time.UTC), Name: "oBDEngineLoad", ValueNumber: 12.54912, ValueString: "", Source: "dimo/integration/123"},
{TokenID: tokenID, Timestamp: time.Date(2024, time.April, 18, 17, 20, 29, 314000000, time.UTC), Name: "obdEngineLoad", ValueNumber: 12.54912, ValueString: "", Source: "dimo/integration/123"},
{TokenID: tokenID, Timestamp: time.Date(2024, time.April, 18, 17, 20, 29, 844000000, time.UTC), Name: "powertrainCombustionEngineTPS", ValueNumber: 23.529600000000002, ValueString: "", Source: "dimo/integration/123"},
{TokenID: tokenID, Timestamp: time.Date(2024, time.April, 18, 17, 20, 37, 235000000, time.UTC), Name: "powertrainCombustionEngineTPS", ValueNumber: 23.529600000000002, ValueString: "", Source: "dimo/integration/123"},
{TokenID: tokenID, Timestamp: time.Date(2024, time.April, 18, 17, 20, 42, 256000000, time.UTC), Name: "powertrainCombustionEngineMAF", ValueNumber: 475.79, ValueString: "", Source: "dimo/integration/123"},
{TokenID: tokenID, Timestamp: time.Date(2024, time.April, 18, 17, 20, 44, 422000000, time.UTC), Name: "oBDEngineLoad", ValueNumber: 12.54912, ValueString: "", Source: "dimo/integration/123"},
{TokenID: tokenID, Timestamp: time.Date(2024, time.April, 18, 17, 20, 44, 422000000, time.UTC), Name: "obdEngineLoad", ValueNumber: 12.54912, ValueString: "", Source: "dimo/integration/123"},
{TokenID: tokenID, Timestamp: time.Date(2024, time.April, 18, 17, 20, 44, 962000000, time.UTC), Name: "powertrainCombustionEngineTPS", ValueNumber: 23.529600000000002, ValueString: "", Source: "dimo/integration/123"},
{TokenID: tokenID, Timestamp: time.Date(2024, time.April, 18, 17, 20, 46, 435000000, time.UTC), Name: "currentLocationLongitude", ValueNumber: -56.50151833333334, ValueString: "", Source: "dimo/integration/123"},
{TokenID: tokenID, Timestamp: time.Date(2024, time.April, 18, 17, 20, 46, 435000000, time.UTC), Name: "currentLocationLatitude", ValueNumber: 56.27014, ValueString: "", Source: "dimo/integration/123"},
{TokenID: tokenID, Timestamp: time.Date(2024, time.April, 18, 17, 20, 46, 435000000, time.UTC), Name: "dIMOAftermarketHDOP", ValueNumber: 1.4, ValueString: "", Source: "dimo/integration/123"},
{TokenID: tokenID, Timestamp: time.Date(2024, time.April, 18, 17, 20, 46, 435000000, time.UTC), Name: "dIMOAftermarketNSAT", ValueNumber: 6, ValueString: "", Source: "dimo/integration/123"},
{TokenID: tokenID, Timestamp: time.Date(2024, time.April, 18, 17, 20, 46, 435000000, time.UTC), Name: "dIMOAftermarketWPAState", ValueNumber: 0, ValueString: "COMPLETED", Source: "dimo/integration/123"},
{TokenID: tokenID, Timestamp: time.Date(2024, time.April, 18, 17, 20, 46, 435000000, time.UTC), Name: "dIMOAftermarketSSID", ValueNumber: 0, ValueString: "foo", Source: "dimo/integration/123"},
{TokenID: tokenID, Timestamp: time.Date(2024, time.April, 18, 17, 20, 46, 435000000, time.UTC), Name: "dimoAftermarketHDOP", ValueNumber: 1.4, ValueString: "", Source: "dimo/integration/123"},
{TokenID: tokenID, Timestamp: time.Date(2024, time.April, 18, 17, 20, 46, 435000000, time.UTC), Name: "dimoAftermarketNSAT", ValueNumber: 6, ValueString: "", Source: "dimo/integration/123"},
{TokenID: tokenID, Timestamp: time.Date(2024, time.April, 18, 17, 20, 46, 435000000, time.UTC), Name: "dimoAftermarketWPAState", ValueNumber: 0, ValueString: "COMPLETED", Source: "dimo/integration/123"},
{TokenID: tokenID, Timestamp: time.Date(2024, time.April, 18, 17, 20, 46, 435000000, time.UTC), Name: "dimoAftermarketSSID", ValueNumber: 0, ValueString: "foo", Source: "dimo/integration/123"},
}
16 changes: 8 additions & 8 deletions pkg/vss/vehicle-structs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 8 additions & 8 deletions pkg/vss/vehicle-v1-convert.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 8 additions & 8 deletions pkg/vss/vehicle-v2-convert.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 670922e

Please sign in to comment.