mirror of
https://github.com/yusing/godoxy.git
synced 2026-04-21 00:11:42 +02:00
feat(serialization): implement Gin JSON/YAML binding
- Introduce SubstituteEnvReader that replaces ${VAR} patterns with environment variable
values, properly quoted for JSON/YAML compatibility
- Gin bindings (JSON/YAML) that use the environment-substituting reader
for request body binding with validation support
This commit is contained in:
37
internal/serialization/gin_binding.go
Normal file
37
internal/serialization/gin_binding.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package serialization
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/bytedance/sonic"
|
||||
"github.com/goccy/go-yaml"
|
||||
)
|
||||
|
||||
type (
|
||||
GinJSONBinding struct{}
|
||||
GinYAMLBinding struct{}
|
||||
)
|
||||
|
||||
func (b GinJSONBinding) Name() string {
|
||||
return "json"
|
||||
}
|
||||
|
||||
func (b GinJSONBinding) Bind(req *http.Request, obj any) error {
|
||||
m := make(map[string]any)
|
||||
if err := sonic.ConfigDefault.NewDecoder(NewSubstituteEnvReader(req.Body)).Decode(&m); err != nil {
|
||||
return err
|
||||
}
|
||||
return MapUnmarshalValidate(m, obj)
|
||||
}
|
||||
|
||||
func (b GinYAMLBinding) Name() string {
|
||||
return "yaml"
|
||||
}
|
||||
|
||||
func (b GinYAMLBinding) Bind(req *http.Request, obj any) error {
|
||||
m := make(map[string]any)
|
||||
if err := yaml.NewDecoder(NewSubstituteEnvReader(req.Body)).Decode(&m); err != nil {
|
||||
return err
|
||||
}
|
||||
return MapUnmarshalValidate(m, obj)
|
||||
}
|
||||
Reference in New Issue
Block a user