diff --git a/check_valid.go b/check_valid.go index d900714..e40d72a 100644 --- a/check_valid.go +++ b/check_valid.go @@ -11,8 +11,10 @@ import ( func Validate(schema string, dataAny any) error { data := StructToMapRecursive(dataAny) + content := []byte(schema) + var v any - if err := json.Unmarshal([]byte(schema), &v); err != nil { + if err := json.Unmarshal(content, &v); err != nil { log.Printf("Failed to decode json %#v", err) return err diff --git a/struct.go b/struct.go index 4458c8c..11e31b6 100644 --- a/struct.go +++ b/struct.go @@ -132,6 +132,37 @@ func StructToMapRecursive(obj any) any { } } +func BsonDGetAny(d bson.D, key string) (any, bool) { + for _, e := range d { + if e.Key == key { + return e.Value, true + } + } + + return nil, false +} + +func BsonDGet[T any](d bson.D, key string) (T, bool) { + var zero T + + for _, e := range d { + if e.Key == key { + val, ok := e.Value.(T) + if !ok { + return zero, false + } + + return val, true + } + } + + return zero, false +} + +func BsonDGetString(d bson.D, key string) (string, bool) { + return BsonDGet[string](d, key) +} + func BsonToStruct(m bson.M, o any) error { b, err := bson.Marshal(m) if err != nil {