Skip to content

Commit

Permalink
Accept all dimensionless measurements
Browse files Browse the repository at this point in the history
Trim space and normalize decimal separators for all cases. Relax the
parsing of measurements to accept all number-like values, not just those
with a single digit.
  • Loading branch information
hansmi committed Jul 4, 2023
1 parent 4768530 commit 25fc952
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 15 deletions.
22 changes: 9 additions & 13 deletions luxwslang/terminology.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,17 @@ func (*Terminology) ParseDuration(v string) (time.Duration, error) {
return time.ParseDuration(fmt.Sprintf("%dh%dm%ds", hours, minutes, seconds))
}

// ParseMeasurement parses a string with a value and a physical unit such as
// degrees Celsius or kWh. The unit is case-sensitive. The returned unit string
// is in a normalized form.
// ParseMeasurement parses a string with a value and an optional physical unit
// such as degrees Celsius or kWh. The unit name is case-sensitive. The
// returned unit string is in a normalized form.
func (*Terminology) ParseMeasurement(text string) (float64, string, error) {
text = strings.TrimSpace(strings.ReplaceAll(text, ",", "."))

if len(text) > 2 {
var value float64
var unit string
var ok bool

text = strings.TrimSpace(strings.ReplaceAll(text, ",", "."))

for _, format := range []string{
"%f %s\n",
"%f%s\n",
Expand Down Expand Up @@ -120,14 +120,10 @@ func (*Terminology) ParseMeasurement(text string) (float64, string, error) {
}
}

if _, err := strconv.Atoi(text); err == nil && len(text) == 1 {
var value float64
var unit string

unit = "enum"
value, _ = strconv.ParseFloat(text, 64)

return value, unit, nil
// Heat pumps of type LD7 report a "Smart Grid" measurement which is
// a dimensionless enumeration.
if value, err := strconv.ParseFloat(text, 64); err == nil {
return value, "", nil
}

return 0, "", fmt.Errorf("unrecognized measurement format %q", text)
Expand Down
5 changes: 3 additions & 2 deletions luxwslang/terminology_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func TestParseMeasurement(t *testing.T) {
wantErr bool
}{
{terms: German, input: "", wantErr: true},
{terms: German, input: "1.23", wantErr: true},
{terms: German, input: "1.23", want: 1.23},
{terms: German, input: "100m", wantErr: true},
{terms: German, input: "1l", wantErr: true},
{terms: German, input: "1.11°C", want: 1.11, wantUnit: "degC"},
Expand All @@ -138,11 +138,12 @@ func TestParseMeasurement(t *testing.T) {
{terms: German, input: "100000 kWh", want: 100000, wantUnit: "kWh"},
{terms: German, input: "1 kW", want: 1, wantUnit: "kW"},
{terms: German, input: "16.66 Hz", want: 16.66, wantUnit: "Hz"},
{terms: German, input: "2", want: 2, wantUnit: "enum"},
{terms: German, input: "2", want: 2},
{terms: English, input: "200 mA", want: 200, wantUnit: "mA"},
{terms: English, input: "3600s", want: 3600, wantUnit: "s"},
{terms: English, input: "36 m³/h", want: 36, wantUnit: "m³/h"},
{terms: English, input: "18 min", want: 18 * 60, wantUnit: "s"},
{terms: English, input: "3.14", want: 3.14},
{terms: Dutch, input: "--- l/h", want: 0, wantUnit: "l/h"},
{terms: English, input: "---rpm", want: 0, wantUnit: "rpm"},
} {
Expand Down

0 comments on commit 25fc952

Please sign in to comment.