Merge pull request #120 from mirdhyn/ignored-tag
Ignore field when tag is "-" when decoding structs
This commit is contained in:
commit
eb82ad4309
@ -517,6 +517,12 @@ func (d *decoder) decodeStruct(name string, node ast.Node, result reflect.Value)
|
|||||||
structType := structVal.Type()
|
structType := structVal.Type()
|
||||||
for i := 0; i < structType.NumField(); i++ {
|
for i := 0; i < structType.NumField(); i++ {
|
||||||
fieldType := structType.Field(i)
|
fieldType := structType.Field(i)
|
||||||
|
tagParts := strings.Split(fieldType.Tag.Get(tagName), ",")
|
||||||
|
|
||||||
|
// Ignore fields with tag name "-"
|
||||||
|
if tagParts[0] == "-" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
if fieldType.Anonymous {
|
if fieldType.Anonymous {
|
||||||
fieldKind := fieldType.Type.Kind()
|
fieldKind := fieldType.Type.Kind()
|
||||||
@ -531,7 +537,6 @@ func (d *decoder) decodeStruct(name string, node ast.Node, result reflect.Value)
|
|||||||
// We have an embedded field. We "squash" the fields down
|
// We have an embedded field. We "squash" the fields down
|
||||||
// if specified in the tag.
|
// if specified in the tag.
|
||||||
squash := false
|
squash := false
|
||||||
tagParts := strings.Split(fieldType.Tag.Get(tagName), ",")
|
|
||||||
for _, tag := range tagParts[1:] {
|
for _, tag := range tagParts[1:] {
|
||||||
if tag == "squash" {
|
if tag == "squash" {
|
||||||
squash = true
|
squash = true
|
||||||
|
@ -417,9 +417,12 @@ func TestDecode_flatMap(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestDecode_structure(t *testing.T) {
|
func TestDecode_structure(t *testing.T) {
|
||||||
|
type Embedded interface{}
|
||||||
|
|
||||||
type V struct {
|
type V struct {
|
||||||
Key int
|
Embedded `hcl:"-"`
|
||||||
Foo string
|
Key int
|
||||||
|
Foo string
|
||||||
}
|
}
|
||||||
|
|
||||||
var actual V
|
var actual V
|
||||||
|
Loading…
x
Reference in New Issue
Block a user