1// Copyright (c) 2014 Couchbase, Inc. 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15package mapping 16 17import ( 18 "reflect" 19 "strings" 20) 21 22func lookupPropertyPath(data interface{}, path string) interface{} { 23 pathParts := decodePath(path) 24 25 current := data 26 for _, part := range pathParts { 27 current = lookupPropertyPathPart(current, part) 28 if current == nil { 29 break 30 } 31 } 32 33 return current 34} 35 36func lookupPropertyPathPart(data interface{}, part string) interface{} { 37 val := reflect.ValueOf(data) 38 typ := val.Type() 39 switch typ.Kind() { 40 case reflect.Map: 41 // FIXME can add support for other map keys in the future 42 if typ.Key().Kind() == reflect.String { 43 key := reflect.ValueOf(part) 44 entry := val.MapIndex(key) 45 if entry.IsValid() { 46 return entry.Interface() 47 } 48 } 49 case reflect.Struct: 50 field := val.FieldByName(part) 51 if field.IsValid() && field.CanInterface() { 52 return field.Interface() 53 } 54 case reflect.Ptr: 55 ptrElem := val.Elem() 56 if ptrElem.IsValid() && ptrElem.CanInterface() { 57 return lookupPropertyPathPart(ptrElem.Interface(), part) 58 } 59 } 60 return nil 61} 62 63const pathSeparator = "." 64 65func decodePath(path string) []string { 66 return strings.Split(path, pathSeparator) 67} 68 69func encodePath(pathElements []string) string { 70 return strings.Join(pathElements, pathSeparator) 71} 72 73func mustString(data interface{}) (string, bool) { 74 if data != nil { 75 str, ok := data.(string) 76 if ok { 77 return str, true 78 } 79 } 80 return "", false 81} 82 83// parseTagName extracts the field name from a struct tag 84func parseTagName(tag string) string { 85 if idx := strings.Index(tag, ","); idx != -1 { 86 return tag[:idx] 87 } 88 return tag 89} 90