mirror of
https://github.com/hanzoai/o11y.git
synced 2026-10-15 12:58:33 +00:00
gin was in go.mod for two reflection helpers and zero routing: pkg/http/binding/query.go ginbinding.MapFormWithTag pkg/valuer/valuer.go ginbinding.BindUnmarshaler BindUnmarshaler was one method; the Valuer contract now states it directly. MapFormWithTag is reimplemented in pkg/http/binding/values.go, which is where it belonged: a binding package should not import a web framework to reflect over form values. bindValues is the whole mapper. It carries exactly the semantics 20 bound structs across this repo actually depend on, enumerated field by field: string · bool · int/8/16/32/64 · uint/8/16/32/64 · float32/64 slices · pointers (nil when the key is absent, allocated when it is not) anonymous embedded structs, flattened into the parent namespace `,default=` (4 live sites: format, limit, order, orderBy) `-` to skip, empty tag falls back to the field name Unmarshaler — the one extension point, which is how every valuer enum binds Dropped as dead weight, none of it reachable from any bound struct: map targets, multipart, time_format/time_utc/time_location, collection_format, the `parser` tag, arrays, and the JSON fallback for unrecognised structs. An unsupported kind is now a named error instead of a silent JSON decode. `required:"..."` is openapi metadata — MapFormWithTag never read it and neither does this; a test pins that so nobody re-derives it. Verified against the code being deleted, not asserted: a differential test ran gin's MapFormWithTag and bindValues over all 20 real query structs plus an every-kind fixture, 3520 input cases, comparing decoded structs and error-ness — identical throughout. Two deliberate mutations (the empty-value default rule, the pointer-allocation rule) turned it red, so it bites. That harness is removed here with gin; 82 table cases stand in its place. go mod tidy also drops goccy/go-yaml, ugorji/go/codec, pelletier/go-toml and mongo-driver, which were gin codec plugins and nothing else. Route table unchanged at 367. Build and full suite green. Co-authored-by: Hanzo Dev <[email protected]>
88 lines
1.8 KiB
Go
88 lines
1.8 KiB
Go
package valuer
|
|
|
|
import (
|
|
"database/sql/driver"
|
|
"encoding/json"
|
|
"reflect"
|
|
"strings"
|
|
|
|
"github.com/hanzoai/o11y/pkg/errors"
|
|
)
|
|
|
|
var _ Valuer = (*String)(nil)
|
|
|
|
type String struct {
|
|
val string
|
|
}
|
|
|
|
func NewString(val string) String {
|
|
return String{val: strings.ToLower(strings.TrimSpace(val))}
|
|
}
|
|
|
|
func (enum String) IsZero() bool {
|
|
return enum.val == ""
|
|
}
|
|
|
|
func (enum String) StringValue() string {
|
|
return enum.val
|
|
}
|
|
|
|
func (enum String) String() string {
|
|
return enum.val
|
|
}
|
|
|
|
func (enum String) MarshalJSON() ([]byte, error) {
|
|
return json.Marshal(enum.StringValue())
|
|
}
|
|
|
|
func (enum *String) UnmarshalJSON(data []byte) error {
|
|
var str string
|
|
if err := json.Unmarshal(data, &str); err != nil {
|
|
return err
|
|
}
|
|
|
|
*enum = NewString(str)
|
|
return nil
|
|
}
|
|
|
|
func (enum String) Value() (driver.Value, error) {
|
|
return enum.StringValue(), nil
|
|
}
|
|
|
|
func (enum *String) Scan(val interface{}) error {
|
|
if enum == nil {
|
|
return errors.Newf(errors.TypeInternal, ErrCodeUnknownValuerScan, "string: (nil \"%s\")", reflect.TypeOf(enum).String())
|
|
}
|
|
|
|
if val == nil {
|
|
// Return an empty string
|
|
*enum = NewString("")
|
|
return nil
|
|
}
|
|
|
|
str, ok := val.(string)
|
|
if !ok {
|
|
return errors.Newf(errors.TypeInternal, ErrCodeUnknownValuerScan, "string: (non-string \"%s\")", reflect.TypeOf(val).String())
|
|
}
|
|
|
|
*enum = NewString(str)
|
|
return nil
|
|
}
|
|
|
|
func (enum *String) UnmarshalText(text []byte) error {
|
|
*enum = NewString(string(text))
|
|
return nil
|
|
}
|
|
|
|
func (enum String) MarshalText() (text []byte, err error) {
|
|
return []byte(enum.StringValue()), nil
|
|
}
|
|
|
|
// UnmarshalParam takes the raw param verbatim: a String enum is normalized by
|
|
// NewString, not rejected, so an unknown value fails validation rather than
|
|
// binding. See https://github.com/hanzoai/o11y/pull/10219 for the background.
|
|
func (enum *String) UnmarshalParam(param string) error {
|
|
*enum = NewString(param)
|
|
return nil
|
|
}
|