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

feat: Added custom Tag Option #208

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
27 changes: 16 additions & 11 deletions copier.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"reflect"
"strings"
"sync"
"unicode"
)

// These flags define options for tag handling
Expand Down Expand Up @@ -44,6 +43,8 @@ type Option struct {
// Custom field name mappings to copy values with different names in `fromValue` and `toValue` types.
// Examples can be found in `copier_field_name_mapping_test.go`.
FieldNameMapping []FieldNameMapping
// Tag
Tag string
}

func (opt Option) converters() map[converterPair]TypeConverter {
Expand Down Expand Up @@ -304,9 +305,11 @@ func copier(toValue interface{}, fromValue interface{}, opt Option) (err error)
initDest = true
dest = indirect(reflect.New(toType))
}

if opt.Tag == "" {
opt.Tag = "copier"
}
// Get tag options
flgs, err := getFlags(dest, source, toType, fromType)
flgs, err := getFlags(dest, source, toType, fromType, opt.Tag)
if err != nil {
return err
}
Expand Down Expand Up @@ -694,18 +697,20 @@ func parseTags(tag string) (flg uint8, name string, err error) {
case "nopanic":
flg = flg | tagNoPanic
default:
if unicode.IsUpper([]rune(t)[0]) {
name = strings.TrimSpace(t)
} else {
err = ErrFieldNameTagStartNotUpperCase
}
name = strings.TrimSpace(t)
// if unicode.IsUpper([]rune(t)[0]) {
// name = strings.TrimSpace(t)
// } else {
// err = ErrFieldNameTagStartNotUpperCase
// }
}
return
}
return
}

// getTagFlags Parses struct tags for bit flags, field name.
func getFlags(dest, src reflect.Value, toType, fromType reflect.Type) (flags, error) {
func getFlags(dest, src reflect.Value, toType, fromType reflect.Type, tag string) (flags, error) {
flgs := flags{
BitFlags: map[string]uint8{},
SrcNames: tagNameMapping{
Expand All @@ -727,7 +732,7 @@ func getFlags(dest, src reflect.Value, toType, fromType reflect.Type) (flags, er

// Get a list dest of tags
for _, field := range toTypeFields {
tags := field.Tag.Get("copier")
tags := field.Tag.Get(tag)
if tags != "" {
var name string
var err error
Expand All @@ -742,7 +747,7 @@ func getFlags(dest, src reflect.Value, toType, fromType reflect.Type) (flags, er

// Get a list source of tags
for _, field := range fromTypeFields {
tags := field.Tag.Get("copier")
tags := field.Tag.Get(tag)
if tags != "" {
var name string
var err error
Expand Down