fix get template func (#721)
This commit is contained in:
		
							parent
							
								
									a5c4bea54a
								
							
						
					
					
						commit
						522392c08c
					
				|  | @ -50,6 +50,15 @@ func get(path string, varArgs ...interface{}) (interface{}, error) { | ||||||
| 			} | 			} | ||||||
| 			return nil, &noValueError{fmt.Sprintf("no value exist for key \"%s\" in %v", keys[0], typedObj)} | 			return nil, &noValueError{fmt.Sprintf("no value exist for key \"%s\" in %v", keys[0], typedObj)} | ||||||
| 		} | 		} | ||||||
|  | 	case map[string]string: | ||||||
|  | 		v, ok = typedObj[keys[0]] | ||||||
|  | 		if !ok { | ||||||
|  | 			if defSet { | ||||||
|  | 				return def, nil | ||||||
|  | 			} | ||||||
|  | 			return nil, &noValueError{fmt.Sprintf("no value exist for key \"%s\" in %v", keys[0], typedObj)} | ||||||
|  | 		} | ||||||
|  | 		return v, nil | ||||||
| 	case map[interface{}]interface{}: | 	case map[interface{}]interface{}: | ||||||
| 		v, ok = typedObj[keys[0]] | 		v, ok = typedObj[keys[0]] | ||||||
| 		if !ok { | 		if !ok { | ||||||
|  |  | ||||||
|  | @ -2,12 +2,37 @@ package tmpl | ||||||
| 
 | 
 | ||||||
| import ( | import ( | ||||||
| 	"testing" | 	"testing" | ||||||
|  | 
 | ||||||
|  | 	"github.com/stretchr/testify/require" | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| type EmptyStruct struct { | type EmptyStruct struct { | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func TestGetStruct(t *testing.T) { | func TestComplexStruct(t *testing.T) { | ||||||
|  | 	type CS struct { | ||||||
|  | 		Bar map[string]string | ||||||
|  | 	} | ||||||
|  | 	obj := CS{ | ||||||
|  | 		Bar: map[string]string{ | ||||||
|  | 			"foo": "bar", | ||||||
|  | 		}, | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	existValue, err := get("Bar.foo", obj) | ||||||
|  | 	require.NoErrorf(t, err, "unexpected error: %v", err) | ||||||
|  | 	require.Equalf(t, existValue, "bar", "unexpected value for path Bar.foo in %v: expected=bar, actual=%v", obj, existValue) | ||||||
|  | 
 | ||||||
|  | 	noExistValue, err := get("Bar.baz", obj) | ||||||
|  | 	require.Errorf(t, err, "expected error but was not occurred") | ||||||
|  | 	require.Nilf(t, noExistValue, "expected nil but was not occurred") | ||||||
|  | 
 | ||||||
|  | 	noExistValueWithDefault, err := get("Bar.baz", "default", obj) | ||||||
|  | 	require.NoErrorf(t, err, "unexpected error: %v", err) | ||||||
|  | 	require.Equalf(t, noExistValueWithDefault, "default", "unexpected value for path Bar.baz in %v: expected=default, actual=%v", obj, noExistValueWithDefault) | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func TestGetSimpleStruct(t *testing.T) { | ||||||
| 	type Foo struct{ Bar string } | 	type Foo struct{ Bar string } | ||||||
| 
 | 
 | ||||||
| 	obj := struct{ Foo }{Foo{Bar: "Bar"}} | 	obj := struct{ Foo }{Foo{Bar: "Bar"}} | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue