ref for projection snapshots

This commit is contained in:
mats committed 2026-06-28 18:41:31 +02:00
1 parent 21bcb63440
commit 5a3ffcb6bc
4 files changed
+91 -5

No files matched your search

+58
View File
@@ -116,6 +116,7 @@ func Apply(s Schema, source map[string]any) (Projection, error) {
}
p := Projection{
ItemID: uint32Of(source, s.ItemIdField),
ID: id,
SKU: sku,
Title: stringOf(source, s.TitleField),
@@ -180,6 +181,63 @@ func stringOf(m map[string]any, key string) string {
return fmt.Sprintf("%v", v)
}
// uint32Of returns the value at key as a uint32. Best-effort coercion:
// json.Number / int / int64 / float (integral check) / truthy numeric
// string. Returns 0 for any missing / non-uint32-representable value —
// Projection.ItemID = 0 is the documented "no id" signal that the cart's
// overlay path uses to fall back to PRODUCT_BASE_URL.
func uint32Of(m map[string]any, key string) uint32 {
if key == "" {
return 0
}
v, ok := m[key]
if !ok || v == nil {
return 0
}
switch x := v.(type) {
case uint32:
return x
case uint64:
if x > 0xFFFFFFFF {
return 0 // overflow – treat as unset, downstream falls back
}
return uint32(x)
case int:
if x < 0 || x > 0xFFFFFFFF {
return 0
}
return uint32(x)
case int64:
if x < 0 || x > 0xFFFFFFFF {
return 0
}
return uint32(x)
case float64:
if x != float64(uint32(x)) || x < 0 || x > 0xFFFFFFFF {
return 0 // fractional or out-of-range
}
return uint32(x)
case json.Number:
s := x.String()
n, err := strconv.ParseUint(s, 10, 32)
if err != nil {
return 0
}
return uint32(n)
case string:
s := strings.TrimSpace(x)
if s == "" {
return 0
}
n, err := strconv.ParseUint(s, 10, 32)
if err != nil {
return 0
}
return uint32(n)
}
return 0
}
// boolOf returns the value at key coerced through truthy. nil/missing keys
// are false; strings lower-cased and matched against truthy; bools passed
// through; numerics "!= 0"; json.Number goes through Float64.