From 9ec4e86883aedeba40107462b7009915bd75512d Mon Sep 17 00:00:00 2001 From: Thomas Pelletier Date: Thu, 18 Mar 2021 20:42:41 -0400 Subject: [PATCH] Handle struct field name variations --- internal/ast/ast.go | 5 ++++- targets.go | 8 ++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/internal/ast/ast.go b/internal/ast/ast.go index 0ec989e..0cd9f93 100644 --- a/internal/ast/ast.go +++ b/internal/ast/ast.go @@ -9,7 +9,8 @@ type Kind int const ( // meta - Comment Kind = iota + Invalid Kind = iota + Comment Key // top level structures @@ -34,6 +35,8 @@ const ( func (k Kind) String() string { switch k { + case Invalid: + return "Invalid" case Comment: return "Comment" case Key: diff --git a/targets.go b/targets.go index 216701d..333b756 100644 --- a/targets.go +++ b/targets.go @@ -3,6 +3,7 @@ package toml import ( "fmt" "reflect" + "strings" ) type target interface { @@ -358,8 +359,11 @@ func scopeStruct(v reflect.Value, name string) (target, bool, error) { if f.Anonymous { // TODO: handle embedded structs } else { - // TODO: handle names variations - if f.Name == name { + fieldName, ok := f.Tag.Lookup("toml") + if !ok { + fieldName = f.Name + } + if strings.EqualFold(fieldName, name) { return valueTarget(v.Field(i)), true, nil } }