mirror of
https://github.com/yusing/godoxy.git
synced 2026-03-26 11:01:07 +01:00
replace all schema check with go-playground/validator/v10
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
package functional
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"sync"
|
||||
|
||||
"github.com/puzpuzpuz/xsync/v3"
|
||||
@@ -195,31 +194,6 @@ func (m Map[KT, VT]) Has(k KT) bool {
|
||||
return ok
|
||||
}
|
||||
|
||||
// UnmarshalFromYAML unmarshals a yaml byte slice into the map.
|
||||
//
|
||||
// It overwrites all existing key-value pairs in the map.
|
||||
//
|
||||
// Parameters:
|
||||
//
|
||||
// data: yaml byte slice to unmarshal
|
||||
//
|
||||
// Returns:
|
||||
//
|
||||
// error: if the unmarshaling fails
|
||||
func (m Map[KT, VT]) UnmarshalFromYAML(data []byte) error {
|
||||
if m.Size() != 0 {
|
||||
return errors.New("cannot unmarshal into non-empty map")
|
||||
}
|
||||
tmp := make(map[KT]VT)
|
||||
if err := yaml.Unmarshal(data, tmp); err != nil {
|
||||
return err
|
||||
}
|
||||
for k, v := range tmp {
|
||||
m.Store(k, v)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m Map[KT, VT]) String() string {
|
||||
tmp := make(map[KT]VT, m.Size())
|
||||
m.RangeAll(func(k KT, v VT) {
|
||||
|
||||
@@ -2,10 +2,8 @@ package utils
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"os"
|
||||
"sync"
|
||||
"syscall"
|
||||
|
||||
@@ -151,19 +149,3 @@ func Copy(dst *ContextWriter, src *ContextReader) (err error) {
|
||||
func Copy2(ctx context.Context, dst io.Writer, src io.Reader) error {
|
||||
return Copy(&ContextWriter{ctx: ctx, Writer: dst}, &ContextReader{ctx: ctx, Reader: src})
|
||||
}
|
||||
|
||||
func LoadJSON[T any](path string, pointer *T) error {
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return json.Unmarshal(data, pointer)
|
||||
}
|
||||
|
||||
func SaveJSON[T any](path string, pointer *T, perm os.FileMode) error {
|
||||
data, err := json.Marshal(pointer)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return os.WriteFile(path, data, perm)
|
||||
}
|
||||
|
||||
@@ -1,27 +0,0 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"github.com/santhosh-tekuri/jsonschema"
|
||||
)
|
||||
|
||||
var (
|
||||
schemaCompiler = jsonschema.NewCompiler()
|
||||
schemaStorage = make(map[string]*jsonschema.Schema)
|
||||
schemaMu sync.Mutex
|
||||
)
|
||||
|
||||
func GetSchema(path string) *jsonschema.Schema {
|
||||
if schema, ok := schemaStorage[path]; ok {
|
||||
return schema
|
||||
}
|
||||
schemaMu.Lock()
|
||||
defer schemaMu.Unlock()
|
||||
if schema, ok := schemaStorage[path]; ok {
|
||||
return schema
|
||||
}
|
||||
schema := schemaCompiler.MustCompile(path)
|
||||
schemaStorage[path] = schema
|
||||
return schema
|
||||
}
|
||||
@@ -1,10 +1,10 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net"
|
||||
"os"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -12,8 +12,8 @@ import (
|
||||
"unicode"
|
||||
|
||||
"github.com/go-playground/validator/v10"
|
||||
"github.com/santhosh-tekuri/jsonschema"
|
||||
E "github.com/yusing/go-proxy/internal/error"
|
||||
"github.com/yusing/go-proxy/internal/utils/functional"
|
||||
"github.com/yusing/go-proxy/internal/utils/strutils"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
@@ -30,36 +30,6 @@ var (
|
||||
ErrUnknownField = E.New("unknown field")
|
||||
)
|
||||
|
||||
func ValidateYaml(schema *jsonschema.Schema, data []byte) E.Error {
|
||||
var i any
|
||||
|
||||
err := yaml.Unmarshal(data, &i)
|
||||
if err != nil {
|
||||
return E.From(err)
|
||||
}
|
||||
|
||||
m, err := json.Marshal(i)
|
||||
if err != nil {
|
||||
return E.From(err)
|
||||
}
|
||||
|
||||
err = schema.Validate(bytes.NewReader(m))
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
var valErr *jsonschema.ValidationError
|
||||
if !errors.As(err, &valErr) {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
b := E.NewBuilder("yaml validation error")
|
||||
for _, e := range valErr.Causes {
|
||||
b.Adds(e.Message)
|
||||
}
|
||||
return b.Error()
|
||||
}
|
||||
|
||||
// Serialize converts the given data into a map[string]any representation.
|
||||
//
|
||||
// It uses reflection to inspect the data type and handle different kinds of data.
|
||||
@@ -482,10 +452,38 @@ func ConvertString(src string, dst reflect.Value) (convertible bool, convErr E.E
|
||||
return true, Convert(reflect.ValueOf(tmp), dst)
|
||||
}
|
||||
|
||||
func DeserializeJSON(j map[string]string, target any) error {
|
||||
data, err := json.Marshal(j)
|
||||
func DeserializeYAML[T any](data []byte, target T) E.Error {
|
||||
m := make(map[string]any)
|
||||
if err := yaml.Unmarshal(data, m); err != nil {
|
||||
return E.From(err)
|
||||
}
|
||||
return Deserialize(m, target)
|
||||
}
|
||||
|
||||
func DeserializeYAMLMap[V any](data []byte) (_ functional.Map[string, V], err E.Error) {
|
||||
m := make(map[string]any)
|
||||
if err = E.From(yaml.Unmarshal(data, m)); err != nil {
|
||||
return
|
||||
}
|
||||
m2 := make(map[string]V, len(m))
|
||||
if err = Deserialize(m, m2); err != nil {
|
||||
return
|
||||
}
|
||||
return functional.NewMapFrom(m2), nil
|
||||
}
|
||||
|
||||
func LoadJSON[T any](path string, dst *T) error {
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return json.Unmarshal(data, target)
|
||||
return json.Unmarshal(data, dst)
|
||||
}
|
||||
|
||||
func SaveJSON[T any](path string, src *T, perm os.FileMode) error {
|
||||
data, err := json.Marshal(src)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return os.WriteFile(path, data, perm)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user