Skip to content

Commit a807b77

Browse files
committed
fix copilot review comments on json_utils.go
- normalizeDateTimeValue: pad fractional seconds even without timezone suffix - float64→int64: add safe integer range guard (±2^53) - fix reservedExposedNames comment (remove 'Name' not in map) - clarify singularize is intentionally naive (matches Studio Pro)
1 parent f2c8e12 commit a807b77

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

mdl/types/json_utils.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,14 @@ func normalizeDateTimeValue(s string) string {
3636
if dotIdx == -1 {
3737
// No fractional part — insert .0000000 before timezone suffix.
3838
// Search from index 19+ to avoid matching the '-' in the date portion (YYYY-MM-DD).
39-
if len(s) > 19 {
39+
if len(s) >= 19 {
4040
if idx := strings.IndexAny(s[19:], "Z+-"); idx >= 0 {
4141
pos := 19 + idx
4242
return s[:pos] + ".0000000" + s[pos:]
4343
}
4444
}
45-
return s
45+
// No timezone suffix — append fractional part at end
46+
return s + ".0000000"
4647
}
4748
// Find where fractional digits end (at Z, +, - or end of string)
4849
fracEnd := len(s)
@@ -113,7 +114,7 @@ var reservedExposedNames = map[string]bool{
113114
}
114115

115116
// resolveExposedName returns the custom name if mapped, otherwise capitalizes the JSON key.
116-
// Reserved names (Id, Type, Name) are prefixed with underscore to match Studio Pro behavior.
117+
// Reserved names (Id, Type) are prefixed with underscore to match Studio Pro behavior.
117118
func (b *snippetBuilder) resolveExposedName(jsonKey string) string {
118119
if b.customNameMap != nil {
119120
if custom, ok := b.customNameMap[jsonKey]; ok {
@@ -230,7 +231,7 @@ func (b *snippetBuilder) buildElementFromRawValue(exposedName, path, jsonKey str
230231
return buildValueElement(exposedName, path, primitiveType, fmt.Sprintf("%q", v))
231232
case float64:
232233
// Check the raw JSON text for a decimal point — Go's %v drops ".0" from 41850.0
233-
if v == math.Trunc(v) && !strings.Contains(trimmed, ".") {
234+
if v == math.Trunc(v) && !strings.Contains(trimmed, ".") && v >= -(1<<53) && v <= (1<<53) {
234235
return buildValueElement(exposedName, path, "Integer", fmt.Sprintf("%v", int64(v)))
235236
}
236237
return buildValueElement(exposedName, path, "Decimal", fmt.Sprintf("%v", v))
@@ -348,8 +349,9 @@ func (b *snippetBuilder) buildElementFromRawArray(exposedName, path, jsonKey, ra
348349
return arrayElem
349350
}
350351

351-
// singularize returns a simple singular form by stripping trailing "s".
352-
// Handles common cases: Tags→Tag, Items→Item, Addresses→Addresse.
352+
// singularize returns a naive singular form by stripping trailing "s".
353+
// Handles common cases: Tags→Tag, Items→Item. Known-incorrect for some words
354+
// (e.g. Addresses→Addresse) — this matches Studio Pro's behavior.
353355
func singularize(s string) string {
354356
if len(s) > 1 && strings.HasSuffix(s, "s") {
355357
return s[:len(s)-1]

mdl/types/json_utils_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,13 @@ func TestNormalizeDateTimeValue_NoTimezone(t *testing.T) {
7070
}
7171
}
7272

73+
func TestNormalizeDateTimeValue_NoFractionalNoTimezone(t *testing.T) {
74+
got := normalizeDateTimeValue("2015-05-22T14:56:29")
75+
if got != "2015-05-22T14:56:29.0000000" {
76+
t.Errorf("expected .0000000 appended, got %q", got)
77+
}
78+
}
79+
7380
func TestBuildJsonElementsFromSnippet_SimpleObject(t *testing.T) {
7481
snippet := `{"name": "John", "age": 30, "active": true}`
7582
elems, err := BuildJsonElementsFromSnippet(snippet, nil)

0 commit comments

Comments
 (0)