@@ -20,20 +20,21 @@ import (
2020 "fmt"
2121)
2222
23- // ConvertJSONNumbers recursively traverses a data structure and a corresponding JSON schema.
24- // It converts instances of float64 into int64 or float64 based on the schema's "type" property.
25- func ConvertJSONNumbers (data any , schema map [string ]any ) (any , error ) {
26- if data == nil || schema == nil {
23+ // NormalizeInput recursively traverses a data structure and performs normalization:
24+ // 1. Removes any fields with null values
25+ // 2. Converts instances of float64 into int64 or float64 based on the schema's "type" property
26+ func NormalizeInput (data any , schema map [string ]any ) (any , error ) {
27+ if data == nil {
2728 return data , nil
2829 }
2930
3031 switch d := data .(type ) {
3132 case float64 :
3233 return convertFloat64 (d , schema )
3334 case map [string ]any :
34- return convertObjectNumbers (d , schema )
35+ return normalizeObjectInput (d , schema )
3536 case []any :
36- return convertArrayNumbers (d , schema )
37+ return normalizeArrayInput (d , schema )
3738 default :
3839 return data , nil
3940 }
@@ -60,45 +61,78 @@ func convertFloat64(f float64, schema map[string]any) (any, error) {
6061 }
6162}
6263
63- // convertObjectNumbers converts any float64s in the map values to int64 or float64 based on the schema's "type" property.
64- func convertObjectNumbers (obj map [string ]any , schema map [string ]any ) (map [string ]any , error ) {
65- props , ok := schema ["properties" ].(map [string ]any )
66- if ! ok {
67- return obj , nil // No properties to guide conversion
64+ // normalizeObjectInput normalizes map values by removing null fields and converting JSON numbers.
65+ func normalizeObjectInput (obj map [string ]any , schema map [string ]any ) (map [string ]any , error ) {
66+ var props map [string ]any
67+ if schema != nil {
68+ props , _ = schema ["properties" ].(map [string ]any )
69+ }
70+
71+ // If no schema or no properties, just remove null fields and normalize recursively
72+ if schema == nil || props == nil {
73+ newObj := make (map [string ]any )
74+ for k , v := range obj {
75+ if v != nil {
76+ normalized , err := NormalizeInput (v , nil )
77+ if err != nil {
78+ return nil , err
79+ }
80+ newObj [k ] = normalized
81+ }
82+ }
83+ return newObj , nil
6884 }
6985
70- newObj := make (map [string ]any , len ( obj ) )
86+ newObj := make (map [string ]any )
7187 for k , v := range obj {
72- newObj [k ] = v // Copy original value
88+ // Skip null values - this removes the field entirely
89+ if v == nil {
90+ continue
91+ }
7392
7493 propSchema , ok := props [k ].(map [string ]any )
7594 if ! ok {
76- continue // No schema for this property
95+ // No schema for this property, just keep it if not null
96+ normalized , err := NormalizeInput (v , nil )
97+ if err != nil {
98+ return nil , err
99+ }
100+ newObj [k ] = normalized
101+ continue
77102 }
78103
79- converted , err := ConvertJSONNumbers (v , propSchema )
104+ normalized , err := NormalizeInput (v , propSchema )
80105 if err != nil {
81106 return nil , err
82107 }
83- newObj [k ] = converted
108+ newObj [k ] = normalized
84109 }
85110 return newObj , nil
86111}
87112
88- // convertArrayNumbers converts any float64s in the array values to int64 or float64 based on the schema's "type" property .
89- func convertArrayNumbers (arr []any , schema map [string ]any ) ([]any , error ) {
113+ // normalizeArrayInput normalizes array values by converting JSON numbers and handling null elements .
114+ func normalizeArrayInput (arr []any , schema map [string ]any ) ([]any , error ) {
90115 items , ok := schema ["items" ].(map [string ]any )
91116 if ! ok {
92- return arr , nil // No items schema to guide conversion
117+ // No items schema, just normalize each element
118+ newArr := make ([]any , len (arr ))
119+ for i , v := range arr {
120+ normalized , err := NormalizeInput (v , nil )
121+ if err != nil {
122+ return nil , err
123+ }
124+ newArr [i ] = normalized
125+ }
126+ return newArr , nil
93127 }
94128
95129 newArr := make ([]any , len (arr ))
96130 for i , v := range arr {
97- converted , err := ConvertJSONNumbers (v , items )
131+ normalized , err := NormalizeInput (v , items )
98132 if err != nil {
99133 return nil , err
100134 }
101- newArr [i ] = converted
135+ newArr [i ] = normalized
102136 }
103137 return newArr , nil
104138}
0 commit comments