Skip to content

Commit 09925cd

Browse files
authored
fix: correctly populate numeric $value (#11)
* fix: correctly populate numeric $value * fix: code review
1 parent 549486e commit 09925cd

3 files changed

Lines changed: 80 additions & 0 deletions

File tree

parser/json.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"math"
1414
"slices"
1515
"sort"
16+
"strconv"
1617
"strings"
1718

1819
"bennypowers.dev/asimonim/fs"
@@ -248,6 +249,13 @@ func (p *JSONParser) createToken(key, path string, valueMap map[string]any, json
248249
rawValue = value
249250
} else {
250251
rawValue = dollarValue
252+
// Populate Value for numeric primitives so downstream consumers
253+
// (e.g. fallback comparison) have a string representation.
254+
if v, ok := dollarValue.(float64); ok {
255+
value = strconv.FormatFloat(v, 'f', -1, 64)
256+
} else if v, ok := dollarValue.(int); ok {
257+
value = strconv.FormatInt(int64(v), 10)
258+
}
251259
}
252260
} else if dollarRef != nil && opts.SchemaVersion != schema.Draft {
253261
if strVal, ok := dollarRef.(string); ok {

parser/json_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,52 @@ func TestJSONParser_AutoDetectSchema(t *testing.T) {
193193
})
194194
}
195195

196+
func TestJSONParser_NumericValues(t *testing.T) {
197+
mfs := testutil.NewFixtureFS(t, "fixtures/draft/numeric-values", "/test")
198+
199+
p := parser.NewJSONParser()
200+
tokens, err := p.ParseFile(mfs, "/test/tokens.json", parser.Options{
201+
SchemaVersion: schema.Draft,
202+
})
203+
204+
if err != nil {
205+
t.Fatalf("unexpected error: %v", err)
206+
}
207+
208+
if len(tokens) != 4 {
209+
t.Errorf("expected 4 tokens, got %d", len(tokens))
210+
}
211+
212+
// Build lookup by name
213+
tokenByName := make(map[string]string)
214+
for _, tok := range tokens {
215+
tokenByName[tok.Name] = tok.Value
216+
}
217+
218+
// Numeric $value fields must populate Token.Value as a string
219+
tests := []struct {
220+
name string
221+
expected string
222+
}{
223+
{"font-weight-regular", "400"},
224+
{"font-weight-medium", "500"},
225+
{"font-weight-bold", "700"},
226+
{"opacity-half", "0.5"},
227+
}
228+
229+
for _, tt := range tests {
230+
t.Run(tt.name, func(t *testing.T) {
231+
val, ok := tokenByName[tt.name]
232+
if !ok {
233+
t.Fatalf("token %s not found", tt.name)
234+
}
235+
if val != tt.expected {
236+
t.Errorf("Token.Value = %q, want %q", val, tt.expected)
237+
}
238+
})
239+
}
240+
}
241+
196242
func TestJSONParser_SkipPositions(t *testing.T) {
197243
mfs := testutil.NewFixtureFS(t, "fixtures/draft/simple", "/test")
198244
data, err := mfs.ReadFile("/test/tokens.json")
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"font": {
3+
"$type": "fontWeight",
4+
"weight": {
5+
"regular": {
6+
"$value": 400,
7+
"$description": "Regular font weight"
8+
},
9+
"medium": {
10+
"$value": 500,
11+
"$description": "Medium font weight"
12+
},
13+
"bold": {
14+
"$value": 700,
15+
"$description": "Bold font weight"
16+
}
17+
}
18+
},
19+
"opacity": {
20+
"$type": "number",
21+
"half": {
22+
"$value": 0.5,
23+
"$description": "Half opacity"
24+
}
25+
}
26+
}

0 commit comments

Comments
 (0)