stuff
This commit is contained in:
1 parent
5807132ac6
commit
ccb43808fa
4 files changed
+177
-8
No files matched your search
+51
-2
@@ -143,6 +143,9 @@ func Apply(s Schema, source map[string]any) (Projection, error) {
|
||||
TaxClass: tax,
|
||||
InventoryTracked: boolOf(source, s.InventoryTrackedF, truthy),
|
||||
DropShip: boolOf(source, s.DropShipF, truthy),
|
||||
SellerID: uint32Of(source, s.SellerIdField),
|
||||
SellerName: stringOf(source, s.SellerNameField),
|
||||
LeadTimeDays: intOf(source, s.LeadTimeField),
|
||||
Category: stringOf(source, s.CategoryField),
|
||||
Category2: stringOf(source, s.Category2Field),
|
||||
Category3: stringOf(source, s.Category3Field),
|
||||
@@ -260,11 +263,57 @@ func uint32Of(m map[string]any, key string) uint32 {
|
||||
return 0
|
||||
}
|
||||
|
||||
// intOf returns the value at key as an int (floor for fractional floats).
|
||||
// Missing / non-numeric / negative values return 0 — Projection.LeadTimeDays
|
||||
// = 0 is the documented "unset" signal, mirroring uint32Of's contract.
|
||||
func intOf(m map[string]any, key string) int {
|
||||
if key == "" {
|
||||
return 0
|
||||
}
|
||||
v, ok := m[key]
|
||||
if !ok || v == nil {
|
||||
return 0
|
||||
}
|
||||
switch x := v.(type) {
|
||||
case int:
|
||||
if x < 0 {
|
||||
return 0
|
||||
}
|
||||
return x
|
||||
case int64:
|
||||
if x < 0 {
|
||||
return 0
|
||||
}
|
||||
return int(x)
|
||||
case float64:
|
||||
if x < 0 {
|
||||
return 0
|
||||
}
|
||||
return int(x)
|
||||
case json.Number:
|
||||
n, err := x.Int64()
|
||||
if err != nil || n < 0 {
|
||||
return 0
|
||||
}
|
||||
return int(n)
|
||||
case string:
|
||||
s := strings.TrimSpace(x)
|
||||
if s == "" {
|
||||
return 0
|
||||
}
|
||||
n, err := strconv.ParseFloat(s, 64)
|
||||
if err != nil || n < 0 {
|
||||
return 0
|
||||
}
|
||||
return int(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.
|
||||
func boolOf(m map[string]any, key string, truthy []string) bool {
|
||||
if key == "" {
|
||||
func boolOf(m map[string]any, key string, truthy []string) bool { if key == "" {
|
||||
return false
|
||||
}
|
||||
v, ok := m[key]
|
||||
|
||||
Reference in new issue
Block a user