37 lines
558 B
Go
37 lines
558 B
Go
package codec
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"reflect"
|
|
)
|
|
|
|
type pathCodec struct {
|
|
req *http.Request
|
|
}
|
|
|
|
// NewPathCodec ...
|
|
func NewPathCodec(r *http.Request) *pathCodec {
|
|
c := &pathCodec{req: r}
|
|
return c
|
|
}
|
|
|
|
// Decode ...
|
|
func (c *pathCodec) Decode(field reflect.StructField, fv reflect.Value, tag *Tag) error {
|
|
pv := c.req.PathValue(tag.Name)
|
|
if pv == "" {
|
|
return nil
|
|
}
|
|
|
|
rv, err := getFieldValue(field.Type, tag, []string{pv})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if !rv.IsValid() {
|
|
return fmt.Errorf("%s not valid", rv)
|
|
}
|
|
|
|
fv.Set(rv)
|
|
return nil
|
|
}
|