feat: add httpc.Do & httpc.Service.Do (#1775)

* backup

* backup

* backup

* feat: add httpc.Do & httpc.Service.Do

* fix: not using strings.Cut, it's from Go 1.18

* chore: remove redudant code

* feat: httpc.Do finished

* chore: fix reviewdog

* chore: break loop if found

* add more tests
This commit is contained in:
Kevin Wan
2022-04-11 11:00:28 +08:00
committed by GitHub
parent fabea4c448
commit 50de01fb49
8 changed files with 680 additions and 10 deletions

176
core/mapping/marshaler.go Normal file
View File

@@ -0,0 +1,176 @@
package mapping
import (
"fmt"
"reflect"
"strings"
)
const (
emptyTag = ""
tagKVSeparator = ":"
)
// Marshal marshals the given val and returns the map that contains the fields.
// optional=another is not implemented, and it's hard to implement and not common used.
func Marshal(val interface{}) (map[string]map[string]interface{}, error) {
ret := make(map[string]map[string]interface{})
tp := reflect.TypeOf(val)
rv := reflect.ValueOf(val)
for i := 0; i < tp.NumField(); i++ {
field := tp.Field(i)
value := rv.Field(i)
if err := processMember(field, value, ret); err != nil {
return nil, err
}
}
return ret, nil
}
func getTag(field reflect.StructField) (string, bool) {
tag := string(field.Tag)
if i := strings.Index(tag, tagKVSeparator); i >= 0 {
return strings.TrimSpace(tag[:i]), true
}
return strings.TrimSpace(tag), false
}
func processMember(field reflect.StructField, value reflect.Value,
collector map[string]map[string]interface{}) error {
var key string
var opt *fieldOptions
var err error
tag, ok := getTag(field)
if !ok {
tag = emptyTag
key = field.Name
} else {
key, opt, err = parseKeyAndOptions(tag, field)
if err != nil {
return err
}
if err = validate(field, value, opt); err != nil {
return err
}
}
val := value.Interface()
if opt != nil && opt.FromString {
val = fmt.Sprint(val)
}
m, ok := collector[tag]
if ok {
m[key] = val
} else {
m = map[string]interface{}{
key: val,
}
}
collector[tag] = m
return nil
}
func validate(field reflect.StructField, value reflect.Value, opt *fieldOptions) error {
if opt == nil || !opt.Optional {
if err := validateOptional(field, value); err != nil {
return err
}
}
if opt == nil {
return nil
}
if len(opt.Options) > 0 {
if err := validateOptions(value, opt); err != nil {
return err
}
}
if opt.Range != nil {
if err := validateRange(value, opt); err != nil {
return err
}
}
return nil
}
func validateOptional(field reflect.StructField, value reflect.Value) error {
switch field.Type.Kind() {
case reflect.Ptr:
if value.IsNil() {
return fmt.Errorf("field %q is nil", field.Name)
}
case reflect.Array, reflect.Slice, reflect.Map:
if value.IsNil() || value.Len() == 0 {
return fmt.Errorf("field %q is empty", field.Name)
}
}
return nil
}
func validateOptions(value reflect.Value, opt *fieldOptions) error {
var found bool
val := fmt.Sprint(value.Interface())
for i := range opt.Options {
if opt.Options[i] == val {
found = true
break
}
}
if !found {
return fmt.Errorf("field %q not in options", val)
}
return nil
}
func validateRange(value reflect.Value, opt *fieldOptions) error {
var val float64
switch v := value.Interface().(type) {
case int:
val = float64(v)
case int8:
val = float64(v)
case int16:
val = float64(v)
case int32:
val = float64(v)
case int64:
val = float64(v)
case uint:
val = float64(v)
case uint8:
val = float64(v)
case uint16:
val = float64(v)
case uint32:
val = float64(v)
case uint64:
val = float64(v)
case float32:
val = float64(v)
case float64:
val = v
default:
return fmt.Errorf("unknown support type for range %q", value.Type().String())
}
// validates [left, right], [left, right), (left, right], (left, right)
if val < opt.Range.left ||
(!opt.Range.leftInclude && val == opt.Range.left) ||
val > opt.Range.right ||
(!opt.Range.rightInclude && val == opt.Range.right) {
return fmt.Errorf("%v out of range", value.Interface())
}
return nil
}

View File

@@ -0,0 +1,233 @@
package mapping
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestMarshal(t *testing.T) {
v := struct {
Name string `path:"name"`
Address string `json:"address,options=[beijing,shanghai]"`
Age int `json:"age"`
Anonymous bool
}{
Name: "kevin",
Address: "shanghai",
Age: 20,
Anonymous: true,
}
m, err := Marshal(v)
assert.Nil(t, err)
assert.Equal(t, "kevin", m["path"]["name"])
assert.Equal(t, "shanghai", m["json"]["address"])
assert.Equal(t, 20, m["json"]["age"].(int))
assert.True(t, m[emptyTag]["Anonymous"].(bool))
}
func TestMarshal_OptionalPtr(t *testing.T) {
var val = 1
v := struct {
Age *int `json:"age"`
}{
Age: &val,
}
m, err := Marshal(v)
assert.Nil(t, err)
assert.Equal(t, 1, *m["json"]["age"].(*int))
}
func TestMarshal_OptionalPtrNil(t *testing.T) {
v := struct {
Age *int `json:"age"`
}{}
_, err := Marshal(v)
assert.NotNil(t, err)
}
func TestMarshal_BadOptions(t *testing.T) {
v := struct {
Name string `json:"name,options"`
}{
Name: "kevin",
}
_, err := Marshal(v)
assert.NotNil(t, err)
}
func TestMarshal_NotInOptions(t *testing.T) {
v := struct {
Name string `json:"name,options=[a,b]"`
}{
Name: "kevin",
}
_, err := Marshal(v)
assert.NotNil(t, err)
}
func TestMarshal_Nested(t *testing.T) {
type address struct {
Country string `json:"country"`
City string `json:"city"`
}
v := struct {
Name string `json:"name,options=[kevin,wan]"`
Address address `json:"address"`
}{
Name: "kevin",
Address: address{
Country: "China",
City: "Shanghai",
},
}
m, err := Marshal(v)
assert.Nil(t, err)
assert.Equal(t, "kevin", m["json"]["name"])
assert.Equal(t, "China", m["json"]["address"].(address).Country)
assert.Equal(t, "Shanghai", m["json"]["address"].(address).City)
}
func TestMarshal_NestedPtr(t *testing.T) {
type address struct {
Country string `json:"country"`
City string `json:"city"`
}
v := struct {
Name string `json:"name,options=[kevin,wan]"`
Address *address `json:"address"`
}{
Name: "kevin",
Address: &address{
Country: "China",
City: "Shanghai",
},
}
m, err := Marshal(v)
assert.Nil(t, err)
assert.Equal(t, "kevin", m["json"]["name"])
assert.Equal(t, "China", m["json"]["address"].(*address).Country)
assert.Equal(t, "Shanghai", m["json"]["address"].(*address).City)
}
func TestMarshal_Slice(t *testing.T) {
v := struct {
Name []string `json:"name"`
}{
Name: []string{"kevin", "wan"},
}
m, err := Marshal(v)
assert.Nil(t, err)
assert.ElementsMatch(t, []string{"kevin", "wan"}, m["json"]["name"].([]string))
}
func TestMarshal_SliceNil(t *testing.T) {
v := struct {
Name []string `json:"name"`
}{
Name: nil,
}
_, err := Marshal(v)
assert.NotNil(t, err)
}
func TestMarshal_Range(t *testing.T) {
v := struct {
Int int `json:"int,range=[1:3]"`
Int8 int8 `json:"int8,range=[1:3)"`
Int16 int16 `json:"int16,range=(1:3]"`
Int32 int32 `json:"int32,range=(1:3)"`
Int64 int64 `json:"int64,range=(1:3)"`
Uint uint `json:"uint,range=[1:3]"`
Uint8 uint8 `json:"uint8,range=[1:3)"`
Uint16 uint16 `json:"uint16,range=(1:3]"`
Uint32 uint32 `json:"uint32,range=(1:3)"`
Uint64 uint64 `json:"uint64,range=(1:3)"`
Float32 float32 `json:"float32,range=(1:3)"`
Float64 float64 `json:"float64,range=(1:3)"`
}{
Int: 1,
Int8: 1,
Int16: 2,
Int32: 2,
Int64: 2,
Uint: 1,
Uint8: 1,
Uint16: 2,
Uint32: 2,
Uint64: 2,
Float32: 2,
Float64: 2,
}
m, err := Marshal(v)
assert.Nil(t, err)
assert.Equal(t, 1, m["json"]["int"].(int))
assert.Equal(t, int8(1), m["json"]["int8"].(int8))
assert.Equal(t, int16(2), m["json"]["int16"].(int16))
assert.Equal(t, int32(2), m["json"]["int32"].(int32))
assert.Equal(t, int64(2), m["json"]["int64"].(int64))
assert.Equal(t, uint(1), m["json"]["uint"].(uint))
assert.Equal(t, uint8(1), m["json"]["uint8"].(uint8))
assert.Equal(t, uint16(2), m["json"]["uint16"].(uint16))
assert.Equal(t, uint32(2), m["json"]["uint32"].(uint32))
assert.Equal(t, uint64(2), m["json"]["uint64"].(uint64))
assert.Equal(t, float32(2), m["json"]["float32"].(float32))
assert.Equal(t, float64(2), m["json"]["float64"].(float64))
}
func TestMarshal_RangeOut(t *testing.T) {
tests := []interface{}{
struct {
Int int `json:"int,range=[1:3]"`
}{
Int: 4,
},
struct {
Int int `json:"int,range=(1:3]"`
}{
Int: 1,
},
struct {
Int int `json:"int,range=[1:3)"`
}{
Int: 3,
},
struct {
Int int `json:"int,range=(1:3)"`
}{
Int: 3,
},
struct {
Bool bool `json:"bool,range=(1:3)"`
}{
Bool: true,
},
}
for _, test := range tests {
_, err := Marshal(test)
assert.NotNil(t, err)
}
}
func TestMarshal_FromString(t *testing.T) {
v := struct {
Age int `json:"age,string"`
}{
Age: 10,
}
m, err := Marshal(v)
assert.Nil(t, err)
assert.Equal(t, "10", m["json"]["age"].(string))
}

View File

@@ -97,10 +97,6 @@ func (u *Unmarshaler) unmarshalWithFullName(m Valuer, v interface{}, fullName st
numFields := rte.NumField()
for i := 0; i < numFields; i++ {
field := rte.Field(i)
if usingDifferentKeys(u.key, field) {
continue
}
if err := u.processField(field, rve.Field(i), m, fullName); err != nil {
return err
}