* feat: 解决goreportcard的警报 ps: warning: if block ends with a return statement, so drop this else and outdent its block (golint) * feat: 优化golint警告,将processFieldPrimitiveWithJsonNumber 改成 processFieldPrimitiveWithJSONNumber unmarshaler.go:248:23: method processFieldPrimitiveWithJsonNumber should be processFieldPrimitiveWithJSONNumber * feat: 添加 WithCanonicalKeyFunc 注释 * feat: 添加DisableStat的注释 * feat: 添加 RegisterGoctlHome 注释 * feat: 添加 PostgreSqlJoin 注释 * feat: 解决goline警告should not use basic type string as key in context.WithValue问题 * feat: 解决golint警告信息: should not use basic type string as key in context.WithValue * fix: 定义自定义字段类型,导致go test fail 问题 * update: 恢复原有测试用例 * fix golint warning
62 lines
1.1 KiB
Go
62 lines
1.1 KiB
Go
package contextx
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestUnmarshalContext(t *testing.T) {
|
|
|
|
type Person struct {
|
|
Name string `ctx:"name"`
|
|
Age int `ctx:"age"`
|
|
}
|
|
|
|
ctx := context.Background()
|
|
ctx = context.WithValue(ctx, "name", "kevin")
|
|
ctx = context.WithValue(ctx, "age", 20)
|
|
|
|
var person Person
|
|
err := For(ctx, &person)
|
|
|
|
assert.Nil(t, err)
|
|
assert.Equal(t, "kevin", person.Name)
|
|
assert.Equal(t, 20, person.Age)
|
|
}
|
|
|
|
func TestUnmarshalContextWithOptional(t *testing.T) {
|
|
type Person struct {
|
|
Name string `ctx:"name"`
|
|
Age int `ctx:"age,optional"`
|
|
}
|
|
|
|
ctx := context.Background()
|
|
ctx = context.WithValue(ctx, "name", "kevin")
|
|
|
|
var person Person
|
|
err := For(ctx, &person)
|
|
|
|
assert.Nil(t, err)
|
|
assert.Equal(t, "kevin", person.Name)
|
|
assert.Equal(t, 0, person.Age)
|
|
}
|
|
|
|
func TestUnmarshalContextWithMissing(t *testing.T) {
|
|
type Person struct {
|
|
Name string `ctx:"name"`
|
|
Age int `ctx:"age"`
|
|
}
|
|
type name string
|
|
const PersonNameKey name = "name"
|
|
|
|
ctx := context.Background()
|
|
ctx = context.WithValue(ctx, PersonNameKey, "kevin")
|
|
|
|
var person Person
|
|
err := For(ctx, &person)
|
|
|
|
assert.NotNil(t, err)
|
|
}
|