removed intermediate /internal directory
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
package param
|
||||
|
||||
import (
|
||||
"encoding"
|
||||
"strconv"
|
||||
|
||||
"ruben/inventory2/domains/accounts"
|
||||
)
|
||||
|
||||
// encoding.TextUnmarshaler instances for Spec
|
||||
|
||||
func Text(dst *string) encoding.TextUnmarshaler {
|
||||
return (*rawText)(dst)
|
||||
}
|
||||
|
||||
func Int(dst *int) encoding.TextUnmarshaler {
|
||||
return (*intText)(dst)
|
||||
}
|
||||
|
||||
func Platform(dst *accounts.Platform) encoding.TextUnmarshaler {
|
||||
return (*platformText)(dst)
|
||||
}
|
||||
|
||||
type (
|
||||
rawText string
|
||||
intText int
|
||||
platformText accounts.Platform
|
||||
)
|
||||
|
||||
func (t *rawText) UnmarshalText(text []byte) error {
|
||||
*t = rawText(text)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (n *intText) UnmarshalText(text []byte) error {
|
||||
i, err := strconv.Atoi(string(text))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
*n = intText(i)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *platformText) UnmarshalText(text []byte) error {
|
||||
v, err := accounts.NewPlatform(string(text))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
*p = platformText(v)
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
package param
|
||||
|
||||
import (
|
||||
"encoding"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"ruben/inventory2/server/response"
|
||||
)
|
||||
|
||||
// Spec is the entry point of the package.
|
||||
// It's typically constructed via Path() or Form().
|
||||
// But it's zero value is valid.
|
||||
//
|
||||
// It's methods support a builder pattern to minimize API bloat.
|
||||
//
|
||||
// To complete gin parameter parsing, call Unmarshal().
|
||||
type Spec struct {
|
||||
path map[string]encoding.TextUnmarshaler
|
||||
form map[string]encoding.TextUnmarshaler
|
||||
}
|
||||
|
||||
func Path(k string, dst encoding.TextUnmarshaler) Spec {
|
||||
return Spec{
|
||||
path: map[string]encoding.TextUnmarshaler{
|
||||
k: dst,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func Form(k string, dst encoding.TextUnmarshaler) Spec {
|
||||
return Spec{
|
||||
form: map[string]encoding.TextUnmarshaler{
|
||||
k: dst,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (s Spec) Path(k string, dst encoding.TextUnmarshaler) Spec {
|
||||
if s.path == nil {
|
||||
s.path = make(map[string]encoding.TextUnmarshaler, 1)
|
||||
}
|
||||
s.path[k] = dst
|
||||
return s
|
||||
}
|
||||
|
||||
func (s Spec) Form(k string, dst encoding.TextUnmarshaler) Spec {
|
||||
if s.form == nil {
|
||||
s.form = make(map[string]encoding.TextUnmarshaler, 1)
|
||||
}
|
||||
s.form[k] = dst
|
||||
return s
|
||||
}
|
||||
|
||||
func (s Spec) Unmarshal(c *gin.Context) error {
|
||||
for k, dst := range s.path {
|
||||
v := c.Param(k)
|
||||
if v == "" {
|
||||
return response.NotFound().Msgf("no %s provided", k)
|
||||
}
|
||||
if err := dst.UnmarshalText([]byte(v)); err != nil {
|
||||
return response.NotFound().Wrap(err).Msgf("invalid %s", k)
|
||||
}
|
||||
}
|
||||
for k, dst := range s.form {
|
||||
v, ok := c.GetPostForm(k)
|
||||
if !ok || v == "" {
|
||||
return response.BadRequest().Msgf("no %s provided", k)
|
||||
}
|
||||
if err := dst.UnmarshalText([]byte(v)); err != nil {
|
||||
return response.BadRequest().Wrap(err).Msgf("invalid %s provided", k)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user