summaryrefslogtreecommitdiff
path: root/vendor/github.com/jinzhu/copier
diff options
context:
space:
mode:
authortomsweeneyredhat <tsweeney@redhat.com>2022-01-26 20:39:58 -0500
committertomsweeneyredhat <tsweeney@redhat.com>2022-01-27 07:03:56 -0500
commit4a4d86d40f9137144103147e7a0ba74fbf3aaca8 (patch)
tree3ac1755da170942000631457d343f9ae2c34ef21 /vendor/github.com/jinzhu/copier
parent09589fccfdd27478defc3e3e3827265d50fa9e33 (diff)
downloadpodman-4a4d86d40f9137144103147e7a0ba74fbf3aaca8.tar.gz
podman-4a4d86d40f9137144103147e7a0ba74fbf3aaca8.tar.bz2
podman-4a4d86d40f9137144103147e7a0ba74fbf3aaca8.zip
Bump Buildah to v1.24.0
Bumps Buildah to v1.24.0 and adopts the new values for pull: true, false, never, and always. The pull-never and pull-always options for the build command are still usable, but they have been removed from the man page documentation with this change. Signed-off-by: tomsweeneyredhat <tsweeney@redhat.com>
Diffstat (limited to 'vendor/github.com/jinzhu/copier')
-rw-r--r--vendor/github.com/jinzhu/copier/copier.go125
1 files changed, 109 insertions, 16 deletions
diff --git a/vendor/github.com/jinzhu/copier/copier.go b/vendor/github.com/jinzhu/copier/copier.go
index 6d21da869..6dc9600c8 100644
--- a/vendor/github.com/jinzhu/copier/copier.go
+++ b/vendor/github.com/jinzhu/copier/copier.go
@@ -24,6 +24,13 @@ const (
// Denotes that the value as been copied
hasCopied
+
+ // Some default converter types for a nicer syntax
+ String string = ""
+ Bool bool = false
+ Int int = 0
+ Float32 float32 = 0
+ Float64 float64 = 0
)
// Option sets copy options
@@ -32,6 +39,18 @@ type Option struct {
// struct having all it's fields set to their zero values respectively (see IsZero() in reflect/value.go)
IgnoreEmpty bool
DeepCopy bool
+ Converters []TypeConverter
+}
+
+type TypeConverter struct {
+ SrcType interface{}
+ DstType interface{}
+ Fn func(src interface{}) (interface{}, error)
+}
+
+type converterPair struct {
+ SrcType reflect.Type
+ DstType reflect.Type
}
// Tag Flags
@@ -59,12 +78,27 @@ func CopyWithOption(toValue interface{}, fromValue interface{}, opt Option) (err
func copier(toValue interface{}, fromValue interface{}, opt Option) (err error) {
var (
- isSlice bool
- amount = 1
- from = indirect(reflect.ValueOf(fromValue))
- to = indirect(reflect.ValueOf(toValue))
+ isSlice bool
+ amount = 1
+ from = indirect(reflect.ValueOf(fromValue))
+ to = indirect(reflect.ValueOf(toValue))
+ converters map[converterPair]TypeConverter
)
+ // save convertes into map for faster lookup
+ for i := range opt.Converters {
+ if converters == nil {
+ converters = make(map[converterPair]TypeConverter)
+ }
+
+ pair := converterPair{
+ SrcType: reflect.TypeOf(opt.Converters[i].SrcType),
+ DstType: reflect.TypeOf(opt.Converters[i].DstType),
+ }
+
+ converters[pair] = opt.Converters[i]
+ }
+
if !to.CanAddr() {
return ErrInvalidCopyDestination
}
@@ -113,13 +147,16 @@ func copier(toValue interface{}, fromValue interface{}, opt Option) (err error)
for _, k := range from.MapKeys() {
toKey := indirect(reflect.New(toType.Key()))
- if !set(toKey, k, opt.DeepCopy) {
+ if !set(toKey, k, opt.DeepCopy, converters) {
return fmt.Errorf("%w map, old key: %v, new key: %v", ErrNotSupported, k.Type(), toType.Key())
}
- elemType, _ := indirectType(toType.Elem())
+ elemType := toType.Elem()
+ if elemType.Kind() != reflect.Slice {
+ elemType, _ = indirectType(elemType)
+ }
toValue := indirect(reflect.New(elemType))
- if !set(toValue, from.MapIndex(k), opt.DeepCopy) {
+ if !set(toValue, from.MapIndex(k), opt.DeepCopy, converters) {
if err = copier(toValue.Addr().Interface(), from.MapIndex(k).Interface(), opt); err != nil {
return err
}
@@ -148,7 +185,7 @@ func copier(toValue interface{}, fromValue interface{}, opt Option) (err error)
to.Set(reflect.Append(to, reflect.New(to.Type().Elem()).Elem()))
}
- if !set(to.Index(i), from.Index(i), opt.DeepCopy) {
+ if !set(to.Index(i), from.Index(i), opt.DeepCopy, converters) {
// ignore error while copy slice element
err = copier(to.Index(i).Addr().Interface(), from.Index(i).Interface(), opt)
if err != nil {
@@ -203,6 +240,8 @@ func copier(toValue interface{}, fromValue interface{}, opt Option) (err error)
// check source
if source.IsValid() {
+ copyUnexportedStructFields(dest, source)
+
// Copy from source field to dest field or method
fromTypeFields := deepFields(fromType)
for _, field := range fromTypeFields {
@@ -249,7 +288,7 @@ func copier(toValue interface{}, fromValue interface{}, opt Option) (err error)
toField := dest.FieldByName(destFieldName)
if toField.IsValid() {
if toField.CanSet() {
- if !set(toField, fromField, opt.DeepCopy) {
+ if !set(toField, fromField, opt.DeepCopy, converters) {
if err := copier(toField.Addr().Interface(), fromField.Interface(), opt); err != nil {
return err
}
@@ -291,7 +330,7 @@ func copier(toValue interface{}, fromValue interface{}, opt Option) (err error)
if toField := dest.FieldByName(destFieldName); toField.IsValid() && toField.CanSet() {
values := fromMethod.Call([]reflect.Value{})
if len(values) >= 1 {
- set(toField, values[0], opt.DeepCopy)
+ set(toField, values[0], opt.DeepCopy, converters)
}
}
}
@@ -303,7 +342,7 @@ func copier(toValue interface{}, fromValue interface{}, opt Option) (err error)
if to.Len() < i+1 {
to.Set(reflect.Append(to, dest.Addr()))
} else {
- if !set(to.Index(i), dest.Addr(), opt.DeepCopy) {
+ if !set(to.Index(i), dest.Addr(), opt.DeepCopy, converters) {
// ignore error while copy slice element
err = copier(to.Index(i).Addr().Interface(), dest.Addr().Interface(), opt)
if err != nil {
@@ -315,7 +354,7 @@ func copier(toValue interface{}, fromValue interface{}, opt Option) (err error)
if to.Len() < i+1 {
to.Set(reflect.Append(to, dest))
} else {
- if !set(to.Index(i), dest, opt.DeepCopy) {
+ if !set(to.Index(i), dest, opt.DeepCopy, converters) {
// ignore error while copy slice element
err = copier(to.Index(i).Addr().Interface(), dest.Interface(), opt)
if err != nil {
@@ -334,6 +373,24 @@ func copier(toValue interface{}, fromValue interface{}, opt Option) (err error)
return
}
+func copyUnexportedStructFields(to, from reflect.Value) {
+ if from.Kind() != reflect.Struct || to.Kind() != reflect.Struct || !from.Type().AssignableTo(to.Type()) {
+ return
+ }
+
+ // create a shallow copy of 'to' to get all fields
+ tmp := indirect(reflect.New(to.Type()))
+ tmp.Set(from)
+
+ // revert exported fields
+ for i := 0; i < to.NumField(); i++ {
+ if tmp.Field(i).CanSet() {
+ tmp.Field(i).Set(to.Field(i))
+ }
+ }
+ to.Set(tmp)
+}
+
func shouldIgnore(v reflect.Value, ignoreEmpty bool) bool {
if !ignoreEmpty {
return false
@@ -352,10 +409,10 @@ func deepFields(reflectType reflect.Type) []reflect.StructField {
// field name. It is empty for upper case (exported) field names.
// See https://golang.org/ref/spec#Uniqueness_of_identifiers
if v.PkgPath == "" {
+ fields = append(fields, v)
if v.Anonymous {
+ // also consider fields of anonymous fields as fields of the root
fields = append(fields, deepFields(v.Type)...)
- } else {
- fields = append(fields, v)
}
}
}
@@ -381,8 +438,14 @@ func indirectType(reflectType reflect.Type) (_ reflect.Type, isPtr bool) {
return reflectType, isPtr
}
-func set(to, from reflect.Value, deepCopy bool) bool {
+func set(to, from reflect.Value, deepCopy bool, converters map[converterPair]TypeConverter) bool {
if from.IsValid() {
+ if ok, err := lookupAndCopyWithConverter(to, from, converters); err != nil {
+ return false
+ } else if ok {
+ return true
+ }
+
if to.Kind() == reflect.Ptr {
// set `to` to nil if from is nil
if from.Kind() == reflect.Ptr && from.IsNil() {
@@ -416,6 +479,9 @@ func set(to, from reflect.Value, deepCopy bool) bool {
toKind = reflect.TypeOf(to.Interface()).Kind()
}
}
+ if from.Kind() == reflect.Ptr && from.IsNil() {
+ return true
+ }
if toKind == reflect.Struct || toKind == reflect.Map || toKind == reflect.Slice {
return false
}
@@ -457,7 +523,7 @@ func set(to, from reflect.Value, deepCopy bool) bool {
to.Set(rv)
}
} else if from.Kind() == reflect.Ptr {
- return set(to, from.Elem(), deepCopy)
+ return set(to, from.Elem(), deepCopy, converters)
} else {
return false
}
@@ -466,6 +532,33 @@ func set(to, from reflect.Value, deepCopy bool) bool {
return true
}
+// lookupAndCopyWithConverter looks up the type pair, on success the TypeConverter Fn func is called to copy src to dst field.
+func lookupAndCopyWithConverter(to, from reflect.Value, converters map[converterPair]TypeConverter) (copied bool, err error) {
+ pair := converterPair{
+ SrcType: from.Type(),
+ DstType: to.Type(),
+ }
+
+ if cnv, ok := converters[pair]; ok {
+ result, err := cnv.Fn(from.Interface())
+
+ if err != nil {
+ return false, err
+ }
+
+ if result != nil {
+ to.Set(reflect.ValueOf(result))
+ } else {
+ // in case we've got a nil value to copy
+ to.Set(reflect.Zero(to.Type()))
+ }
+
+ return true, nil
+ }
+
+ return false, nil
+}
+
// parseTags Parses struct tags and returns uint8 bit flags.
func parseTags(tag string) (flg uint8, name string, err error) {
for _, t := range strings.Split(tag, ",") {