-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add EscapePropertyName and UnescapePropertyName functions to escape d…
…ots in property names. Update GetFieldValueFromInterface and GetNestedFieldValueFromInterface to use UnescapePropertyName to support names with dots. Fixes #21. Closes #23
- Loading branch information
1 parent
af05364
commit 8e2a216
Showing
3 changed files
with
71 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package helpers | ||
|
||
import ( | ||
"reflect" | ||
"strings" | ||
|
||
"github.com/tkrajina/go-reflector/reflector" | ||
) | ||
|
||
// GetFieldValueFromInterface uses reflection to return the value of the given property path | ||
func GetFieldValueFromInterface(i interface{}, fieldName string) (interface{}, bool) { | ||
// unescape property name | ||
fieldName = UnescapePropertyName(fieldName) | ||
obj := reflector.New(i) | ||
if arrayProperty, index, ok := IsFieldArray(fieldName); ok { | ||
if arrVal, ok := GetFieldValueFromInterface(i, arrayProperty); ok { | ||
return GetArrayValue(arrVal, index) | ||
} | ||
return nil, false | ||
} | ||
|
||
if reflect.TypeOf(i).Kind() == reflect.Map { | ||
return obj.GetByKey(fieldName) | ||
} | ||
|
||
val, err := obj.Field(fieldName).Get() | ||
return val, err == nil | ||
} | ||
|
||
// GetNestedFieldValueFromInterface uses reflection to return the value of the given nested property path | ||
func GetNestedFieldValueFromInterface(item interface{}, propertyPath string) (interface{}, bool) { | ||
var value interface{} | ||
var ok bool | ||
parent := item | ||
var pathSegments = strings.Split(propertyPath, ".") | ||
for _, fieldName := range pathSegments { | ||
// if there are any dots encoded in this segment, decode | ||
fieldName = UnescapePropertyName(fieldName) | ||
value, ok = GetFieldValueFromInterface(parent, fieldName) | ||
if !ok { | ||
return nil, false | ||
} | ||
// update parent for next iteration | ||
parent = value | ||
} | ||
return value, true | ||
} | ||
|
||
const propertyPathDotEscape = "$steampipe_escaped_dot$" | ||
|
||
// helpers to support property names containing a dot | ||
|
||
// EscapePropertyName replaces any '.' characters in the property name with propertyPathDotEscape | ||
func EscapePropertyName(name string) string { | ||
return strings.Replace(name, ".", propertyPathDotEscape, -1) | ||
} | ||
|
||
// UnescapePropertyName replaces any propertyPathDotEscape occurrences with "." | ||
func UnescapePropertyName(name string) string { | ||
return strings.Replace(name, propertyPathDotEscape, ".", -1) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters