From 004b63018d8272ac37f16e58c1e80401d67f7a06 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Tue, 24 Feb 2015 22:36:54 +0800 Subject: [PATCH] Add License --- License | 20 ++++++++++ README.md | 12 ++++++ copier.go | 116 ++++++++++++++++++++++++++---------------------------- 3 files changed, 88 insertions(+), 60 deletions(-) create mode 100644 License diff --git a/License b/License new file mode 100644 index 0000000..e2dc538 --- /dev/null +++ b/License @@ -0,0 +1,20 @@ +The MIT License (MIT) + +Copyright (c) 2015 Jinzhu + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md index 79c4c67..7ed6ee6 100644 --- a/README.md +++ b/README.md @@ -63,3 +63,15 @@ Copy(&employees, &users) // employees => [{hello 18 0 36 Super User} {Jinzhu 18 0 36 Super Admin} {jinzhu 2 30 0 60 Super Dev}] ``` + +# Author + +**jinzhu** + +* +* +* + +## License + +Released under the [MIT License](https://github.com/jinzhu/copier/blob/master/License). diff --git a/copier.go b/copier.go index 6dc753f..136a885 100644 --- a/copier.go +++ b/copier.go @@ -2,108 +2,104 @@ package copier import "reflect" -func Copy(copy_to interface{}, copy_from interface{}) (err error) { +func Copy(toValue interface{}, fromValue interface{}) (err error) { var ( - is_slice bool - from_typ reflect.Type - is_from_typ_ptr bool - to_typ reflect.Type - is_to_typ_ptr bool - elem_amount int + isSlice bool + fromType reflect.Type + isFromPtr bool + toType reflect.Type + isToPtr bool + amount int ) - from := reflect.ValueOf(copy_from) - to := reflect.ValueOf(copy_to) - from_elem := reflect.Indirect(from) - to_elem := reflect.Indirect(to) + from := reflect.Indirect(reflect.ValueOf(fromValue)) + to := reflect.Indirect(reflect.ValueOf(toValue)) - if to_elem.Kind() == reflect.Slice { - is_slice = true - if from_elem.Kind() == reflect.Slice { - from_typ = from_elem.Type().Elem() - if from_typ.Kind() == reflect.Ptr { - from_typ = from_typ.Elem() - is_from_typ_ptr = true + if to.Kind() == reflect.Slice { + isSlice = true + if from.Kind() == reflect.Slice { + fromType = from.Type().Elem() + if fromType.Kind() == reflect.Ptr { + fromType = fromType.Elem() + isFromPtr = true } - elem_amount = from_elem.Len() + amount = from.Len() } else { - from_typ = from_elem.Type() - elem_amount = 1 + fromType = from.Type() + amount = 1 } - to_typ = to_elem.Type().Elem() - if to_typ.Kind() == reflect.Ptr { - to_typ = to_typ.Elem() - is_to_typ_ptr = true + toType = to.Type().Elem() + if toType.Kind() == reflect.Ptr { + toType = toType.Elem() + isToPtr = true } - } else { - from_typ = from_elem.Type() - to_typ = to_elem.Type() - elem_amount = 1 + fromType = from.Type() + toType = to.Type() + amount = 1 } - for e := 0; e < elem_amount; e++ { + for e := 0; e < amount; e++ { var dest, source reflect.Value - if is_slice { - if from_elem.Kind() == reflect.Slice { - source = from_elem.Index(e) - if is_from_typ_ptr { + if isSlice { + if from.Kind() == reflect.Slice { + source = from.Index(e) + if isFromPtr { source = source.Elem() } } else { - source = from_elem + source = from } } else { - source = from_elem + source = from } - if is_slice { - dest = reflect.New(to_typ).Elem() + if isSlice { + dest = reflect.New(toType).Elem() } else { - dest = to_elem + dest = to } - for i := 0; i < from_typ.NumField(); i++ { - field := from_typ.Field(i) + for i := 0; i < fromType.NumField(); i++ { + field := fromType.Field(i) if !field.Anonymous { name := field.Name - from_field := source.FieldByName(name) - to_field := dest.FieldByName(name) - to_method := dest.Addr().MethodByName(name) - if from_field.IsValid() && to_field.IsValid() { - to_field.Set(from_field) + fromField := source.FieldByName(name) + toField := dest.FieldByName(name) + toMethod := dest.Addr().MethodByName(name) + if fromField.IsValid() && toField.IsValid() { + toField.Set(fromField) } - if from_field.IsValid() && to_method.IsValid() { - to_method.Call([]reflect.Value{from_field}) + if fromField.IsValid() && toMethod.IsValid() { + toMethod.Call([]reflect.Value{fromField}) } } } - for i := 0; i < dest.NumField(); i++ { - field := to_typ.Field(i) + for i := 0; i < toType.NumField(); i++ { + field := toType.Field(i) if !field.Anonymous { name := field.Name - from_method := source.Addr().MethodByName(name) - to_field := dest.FieldByName(name) + fromMethod := source.Addr().MethodByName(name) + toField := dest.FieldByName(name) - if from_method.IsValid() && to_field.IsValid() { - values := from_method.Call([]reflect.Value{}) + if fromMethod.IsValid() && toField.IsValid() { + values := fromMethod.Call([]reflect.Value{}) if len(values) >= 1 { - to_field.Set(values[0]) + toField.Set(values[0]) } } } } - if is_slice { - if is_to_typ_ptr { - to_elem.Set(reflect.Append(to_elem, dest.Addr())) + if isSlice { + if isToPtr { + to.Set(reflect.Append(to, dest.Addr())) } else { - to_elem.Set(reflect.Append(to_elem, dest)) + to.Set(reflect.Append(to, dest)) } - } } return