Add JsonPrintError
This commit is contained in:
40
json.go
Normal file
40
json.go
Normal 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
41
json_test.go
Normal 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
10
math.go
@@ -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
|
||||||
|
}
|
||||||
15
struct.go
15
struct.go
@@ -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 {
|
||||||
|
|||||||
Reference in New Issue
Block a user