convert

package
v0.0.0-...-d31700d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 28, 2022 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Header(value reflect.Value, map2strs map[string][]string) (err error)

func Query

func Query(value reflect.Value, map2strs map[string][]string) (err error)
Example
var v struct {
	Id    int
	Name  string `json:"userName"`
	Desc  string `json:",omitempty"`
	Array []int  `json:"array,omitempty"`
	X     int
	Y     string `json:"-"`
}
fmt.Println(Query(reflect.ValueOf(&v), map[string][]string{}))
fmt.Printf("%+v\n", v)

fmt.Println(Query(reflect.ValueOf(&v), map[string][]string{
	"id":       {"9"},
	"userName": {"LiLei"},
	"Desc":     {"description"},
	"array[]":  {"1", "2", "3"},
}))
fmt.Printf("%+v\n", v)

fmt.Println(Query(reflect.ValueOf(&v), map[string][]string{
	"x": {"a"},
}))
fmt.Println(v.X)
Output:

<nil>
{Id:0 Name: Desc: Array:[] X:0 Y:}
<nil>
{Id:9 Name:LiLei Desc:description Array:[1 2 3] X:0 Y:}
req.Query.X: strconv.ParseInt: parsing "a": invalid syntax
0

func Session

func Session(sess, value reflect.Value) error

func Set

func Set(v reflect.Value, s string) error
Example
var t struct {
	V string
}
fmt.Println(Set(reflect.ValueOf(&t), "XX"))
fmt.Println(t)

var t2 testUnmarshaler
fmt.Println(Set(reflect.ValueOf(&t2), "XX"))
fmt.Println(t2)
Output:

invalid character 'X' looking for beginning of value
{}
<nil>
{XX}

func SetArray

func SetArray(v reflect.Value, array []string) error
Example
var v = struct {
	Slice []string
	Array [3]int
	Json  []struct {
		Id   int
		Name string
	}
}{}

if err := SetArray(reflect.ValueOf(&v.Slice), []string{"a", "bc", "d"}); err != nil {
	log.Panic(err)
}
if err := SetArray(reflect.ValueOf(&v.Array), []string{"1", "2"}); err != nil {
	log.Panic(err)
}
if err := SetArray(reflect.ValueOf(&v.Json), []string{
	`{"id": 3, "name": "f" }`, "{}",
}); err != nil {
	log.Panic(err)
}
fmt.Println(v.Slice)
fmt.Println(v.Array)
fmt.Println(v.Json)
Output:

[a bc d]
[1 2 0]
[{3 f} {0 }]

func SetFloat

func SetFloat(v reflect.Value, s string) error

func SetInt

func SetInt(v reflect.Value, s string) error

func SetUint

func SetUint(v reflect.Value, s string) error

func Traverse

func Traverse(
	value reflect.Value, convertNilPtr bool, fn func(reflect.Value, reflect.StructField) bool,
)

func TraverseType

func TraverseType(typ reflect.Type, fn func(f reflect.StructField))

func ValidateHeader

func ValidateHeader(typ reflect.Type)

func ValidateQuery

func ValidateQuery(typ reflect.Type)

func ValidateRespHeader

func ValidateRespHeader(typ reflect.Type)

func WriteRespHeader

func WriteRespHeader(value reflect.Value, header http.Header)

Types

type ParamConverter

type ParamConverter struct {
	// contains filtered or unexported fields
}

func GetParamConverter

func GetParamConverter(typ reflect.Type, path string) ParamConverter

func (ParamConverter) Convert

func (pc ParamConverter) Convert(param reflect.Value, paramsSlice []string) error

type ParamField

type ParamField struct {
	ParamIndex int
	reflect.StructField
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL