Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Omitempty #4

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 2 additions & 0 deletions tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type fieldTags struct {
keyFromLabel bool
valueFromLabel bool
seq bool
omitEmpty bool
}

func parseTag(tag reflect.StructTag) fieldTags {
Expand All @@ -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"),
}
}

Expand Down
3 changes: 1 addition & 2 deletions write.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down