diff --git a/README.md b/README.md index 50d9c92..2fd984b 100644 --- a/README.md +++ b/README.md @@ -118,7 +118,8 @@ Other possible (optional) values are: * `seq` (slice field only): will treat field as a seq entry in the Augeas tree; * `key-from-value` (map field only): get the key from the node label instead of its value; -* `purge` (map field only): purge all unknown keys in the map. +* `purge` (map field only): purge all unknown keys in the map; +* `omitempty`: do not set value when not found. ## Fields diff --git a/parse.go b/parse.go index 6018075..af1cd9d 100644 --- a/parse.go +++ b/parse.go @@ -30,7 +30,7 @@ func (n *Narcissus) Parse(val interface{}) error { func (n *Narcissus) parseStruct(ref reflect.Value, path string) error { refType := ref.Type() for i := 0; i < refType.NumField(); i++ { - if refType.Field(i).Name == "augeasPath" { + if strings.HasPrefix(refType.Field(i).Name, "augeas") { // Ignore the special `augeasPath` field continue } diff --git a/tags.go b/tags.go index 768e9cc..9639d3b 100644 --- a/tags.go +++ b/tags.go @@ -13,6 +13,7 @@ type fieldTags struct { keyFromLabel bool valueFromLabel bool seq bool + omitEmpty bool } func parseTag(tag reflect.StructTag) fieldTags { @@ -24,6 +25,7 @@ func parseTag(tag reflect.StructTag) fieldTags { keyFromLabel: sliceHasTag(slice, "key-from-label"), valueFromLabel: sliceHasTag(slice, "value-from-label"), seq: sliceHasTag(slice, "seq"), + omitEmpty: sliceHasTag(slice, "omitempty"), } } diff --git a/write.go b/write.go index 2346f47..af66fbf 100644 --- a/write.go +++ b/write.go @@ -71,8 +71,7 @@ func (n *Narcissus) writeSimpleField(field reflect.Value, fieldPath string, tag return nil } - // FIXME: use omitempty for that - if value == "" { + if value == "" && parseTag(tag).omitEmpty { return nil }