示例#1
0
func TestReflectOrIndirect(t *testing.T) {
	_, err := internal.ReflectOrIndirect(nil)
	require.Error(t, err, "@nil")

	v, err := internal.ReflectOrIndirect(s)
	require.NoError(t, err, "@value")
	require.Equal(t, reflect.String, v.Kind(), "value kind")
	require.Equal(t, s, v.Interface(), "value")

	v, err = internal.ReflectOrIndirect(&s)
	require.NoError(t, err, "@ptr")
	require.Equal(t, reflect.String, v.Kind(), "ptr kind")
	require.Equal(t, s, v.Interface(), "ptr")

	_, err = internal.ReflectOrIndirect(np)
	require.Error(t, err, "@nil ptr")
}
示例#2
0
文件: object.go 项目: vpol/govalid
// the actual workhorse for object validator
func (r *objectValidator) Validate(data interface{}) (string, error) {

	value, err := internal.ReflectOrIndirect(data)
	if err != nil {
		return "Object", fmt.Errorf("expected (*)map, got %s", err)
	}

	if value.Kind() != reflect.Map {
		return "Object", fmt.Errorf("expected (*)map, got %s", value.Type())
	}

	for _, o := range r.opts {
		if path, err := o(value); err != nil {
			return "Object->" + path, err
		}
	}

	return "", nil
}