github.com/Rookout/GoSDK@v0.1.48/pkg/processor/paths/operations.go (about) 1 package paths 2 3 import ( 4 "github.com/Rookout/GoSDK/pkg/processor/namespaces" 5 "github.com/Rookout/GoSDK/pkg/rookoutErrors" 6 "github.com/Rookout/GoSDK/pkg/utils" 7 "strings" 8 ) 9 10 type pathOperation interface { 11 Read(namespace namespaces.Namespace, create bool) (namespaces.Namespace, rookoutErrors.RookoutError) 12 } 13 14 type writeOperation interface { 15 pathOperation 16 Write(namespace namespaces.Namespace, value namespaces.Namespace) rookoutErrors.RookoutError 17 } 18 19 type lookupOperation struct { 20 name interface{} 21 } 22 23 type methodOperation struct { 24 methodName string 25 methodArguments string 26 } 27 28 type attributeOperation struct { 29 name string 30 } 31 32 func newLookupOperation(name string) (pathOperation, rookoutErrors.RookoutError) { 33 a := &lookupOperation{} 34 35 switch { 36 case strings.HasPrefix(name, "'"): 37 a.name = strings.Trim(name, "'") 38 case strings.HasPrefix(name, "\""): 39 a.name = strings.Trim(name, "\"") 40 default: 41 d, err := utils.StringToInt(name) 42 if err != nil { 43 return nil, rookoutErrors.NewBadTypeException("type is not a string", err) 44 } 45 a.name = d 46 } 47 48 return a, nil 49 } 50 51 func (l lookupOperation) Read(namespace namespaces.Namespace, _ bool) (namespaces.Namespace, rookoutErrors.RookoutError) { 52 return namespace.ReadKey(l.name) 53 } 54 55 func newMethodOperation(methodName string, methodArguments string) pathOperation { 56 f := &methodOperation{ 57 methodName: methodName, 58 methodArguments: methodArguments, 59 } 60 61 return f 62 } 63 64 func (f methodOperation) Read(namespace namespaces.Namespace, _ bool) (namespaces.Namespace, rookoutErrors.RookoutError) { 65 return namespace.CallMethod(f.methodName, f.methodArguments) 66 } 67 68 func newAttributeOperation(name string) pathOperation { 69 a := &attributeOperation{ 70 name: name, 71 } 72 73 return a 74 } 75 76 func (a attributeOperation) Read(namespace namespaces.Namespace, create bool) (namespaces.Namespace, rookoutErrors.RookoutError) { 77 if n, err := namespace.ReadAttribute(a.name); err == nil { 78 return n, nil 79 } 80 81 if create { 82 err := namespace.WriteAttribute(a.name, namespaces.NewEmptyContainerNamespace()) 83 if err != nil { 84 return nil, err 85 } 86 return namespace.ReadAttribute(a.name) 87 } 88 89 return nil, rookoutErrors.NewRookAttributeNotFoundException(a.name) 90 } 91 92 func (a attributeOperation) Write(namespace namespaces.Namespace, value namespaces.Namespace) rookoutErrors.RookoutError { 93 return namespace.WriteAttribute(a.name, value) 94 }