MCPcopy Create free account
hub / github.com/blues/note / omitempty

Function omitempty

lib/bulk.go:918–951  ·  view source on GitHub ↗

Eliminate fields from a JSON object in a way that simulates "omitempty" tags, recursively

(in map[string]interface{})

Source from the content-addressed store, hash-verified

916
917// Eliminate fields from a JSON object in a way that simulates "omitempty" tags, recursively
918func omitempty(in map[string]interface{}) (out map[string]interface{}) {
919 out = in
920 for key, value := range in {
921 switch tv := value.(type) {
922 case json.Number:
923 valueAsInt64, err := tv.Int64()
924 if err == nil && valueAsInt64 == 0 {
925 valueAsFloat64, err := tv.Float64()
926 if err == nil && valueAsFloat64 == 0 {
927 delete(out, key)
928 }
929 }
930 case float64:
931 if value == 0.0 {
932 delete(out, key)
933 }
934 case int:
935 if value == 0 {
936 delete(out, key)
937 }
938 case string:
939 if value == "" {
940 delete(out, key)
941 }
942 case bool:
943 if value == false {
944 delete(out, key)
945 }
946 case map[string]interface{}:
947 out[key] = omitempty(value.(map[string]interface{}))
948 }
949 }
950 return
951}
952
953// In the case of the ORIG format, get parameters about bin records from the template
954// because in that format the bin records were not yet self-describing.

Callers 1

BulkDecodeNextEntryMethod · 0.85

Calls 2

Int64Method · 0.80
Float64Method · 0.80

Tested by

no test coverage detected