Add JsonPrintError

This commit is contained in:
George Suntres
2026-04-14 11:29:05 -04:00
parent 2fccb814c3
commit c0bee95cf6
4 changed files with 105 additions and 1 deletions

40
json.go Normal file
View File

@@ -0,0 +1,40 @@
package commons
import (
"log"
"reflect"
"encoding/json"
)
func JsonPrintError(err error, content []byte) {
if syntaxErr, ok := err.(*json.SyntaxError); ok {
totalLen := int64(len(content))
min := MathMin(int64(50), syntaxErr.Offset)
from := MathMax(syntaxErr.Offset - min, 0)
to := MathMin(syntaxErr.Offset + min, totalLen)
sliceBuf := content[from:to]
log.Printf("Failed to unmarshal %#v \n %s", syntaxErr, string(sliceBuf))
} else {
log.Printf("%v", err)
}
}
// JsonEqual check if two json string representations are equal.
func JsonEqual(a, b string) bool {
var o1 any
var o2 any
if err := json.Unmarshal([]byte(a), &o1); err != nil {
return false
}
if err := json.Unmarshal([]byte(b), &o2); err != nil {
return false
}
return reflect.DeepEqual(o1, o2)
}

41
json_test.go Normal file
View File

@@ -0,0 +1,41 @@
package commons
import (
"log"
"fmt"
"bytes"
"encoding/json"
"testing"
)
func TestJsonPrintError(t *testing.T) {
jsonstr := `
{
"name": "George",
"age": 87,
active": "false",
"properties": {
"one": "two",
"three": "four"
}
}
`
var buf bytes.Buffer
log.SetOutput(&buf)
content := []byte(jsonstr)
var data map[string]any
err := json.Unmarshal(content, &data)
JsonPrintError(err, content)
errOut := fmt.Sprintf("%s", buf.String())
if errOut == "" {
t.Fatal("should have printed the error")
}
}

10
math.go
View File

@@ -9,3 +9,13 @@ func MathMin[T ~int | ~int8 | ~int16 | ~int32 | ~int64 |
} }
return b return b
} }
// MathMax will compare two integers and return the one with the smaller value.
func MathMax[T ~int | ~int8 | ~int16 | ~int32 | ~int64 |
~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr |
~float32 | ~float64 | ~string](a, b T) T {
if a > b {
return a
}
return b
}

View File

@@ -4,6 +4,7 @@ import (
"log" "log"
"reflect" "reflect"
"strings" "strings"
"bytes"
"encoding/json" "encoding/json"
"github.com/go-viper/mapstructure/v2" "github.com/go-viper/mapstructure/v2"
@@ -52,8 +53,11 @@ func StructMustToMap(data any) map[string]any {
return nil return nil
} }
dec := json.NewDecoder(bytes.NewReader(b))
dec.UseNumber()
var res map[string]any var res map[string]any
err = json.Unmarshal(b, &res) err = dec.Decode(&res)
if err != nil { if err != nil {
log.Printf("Failed to unmarshal %v", err) log.Printf("Failed to unmarshal %v", err)
@@ -219,6 +223,15 @@ func BsonAToSlice(m any) ([]map[string]any, error) {
return o.Items, nil return o.Items, nil
} }
func MapFromString(s string) (map[string]any, error) {
var b map[string]any
if err := bson.Unmarshal([]byte(s), &b); err != nil {
return nil, err
}
return b, nil
}
// MapIsSubset given two map[string]any m1 and m2 will determine if m1 is a subset of m2. // MapIsSubset given two map[string]any m1 and m2 will determine if m1 is a subset of m2.
// Only fields' name is evaluated not their values. // Only fields' name is evaluated not their values.
func MapIsSubset(subset, superset any) bool { func MapIsSubset(subset, superset any) bool {