github.com/Rookout/GoSDK@v0.1.48/pkg/processor/namespaces/go_object_namespace.go (about) 1 package namespaces 2 3 import ( 4 "container/list" 5 "reflect" 6 7 "github.com/Rookout/GoSDK/pkg/config" 8 "github.com/Rookout/GoSDK/pkg/utils" 9 10 "github.com/Rookout/GoSDK/pkg/rookoutErrors" 11 ) 12 13 type GoObjectNamespace struct { 14 Obj interface{} 15 ObjectDumpConf config.ObjectDumpConfig 16 } 17 18 func NewGoObjectNamespace(o interface{}) *GoObjectNamespace { 19 g := &GoObjectNamespace{ 20 Obj: o, 21 ObjectDumpConf: config.GetDefaultDumpConfig(), 22 } 23 24 return g 25 } 26 27 func (g *GoObjectNamespace) GetSize(_ string, _ string) Namespace { 28 reflectedValue := reflect.ValueOf(g.Obj) 29 if reflectedValue.Kind() == reflect.Ptr { 30 reflectedValue = reflectedValue.Elem() 31 } 32 33 switch reflectedValue.Kind() { 34 case reflect.Array: 35 return NewGoObjectNamespace(reflectedValue.Len()) 36 37 case reflect.Map: 38 return NewGoObjectNamespace(len(reflectedValue.MapKeys())) 39 40 default: 41 42 switch reflect.Zero(reflectedValue.Type()).String() { 43 case "<*list.List Value>": 44 l := reflectedValue.Interface().(*list.List) 45 46 return NewGoObjectNamespace(l.Len()) 47 } 48 } 49 return nil 50 } 51 52 func (g *GoObjectNamespace) CallMethod(name string, args string) (Namespace, rookoutErrors.RookoutError) { 53 switch name { 54 case "type": 55 if nil == g.Obj { 56 return NewGoObjectNamespace("nil"), nil 57 } 58 59 reflectedValue := reflect.ValueOf(g.Obj) 60 61 if reflectedValue.Kind() == reflect.Ptr { 62 reflectedValue = reflectedValue.Elem() 63 } 64 65 x := reflectedValue.Type().String() 66 67 return NewGoObjectNamespace(x), nil 68 case "size": 69 size := g.GetSize(name, args) 70 if size == nil { 71 return nil, rookoutErrors.NewObjectHasNoSizeException(g.GetObject()) 72 } 73 return size, nil 74 75 default: 76 return nil, rookoutErrors.NewRookMethodNotFound(name) 77 } 78 } 79 80 func (g *GoObjectNamespace) ReadAttribute(name string) (Namespace, rookoutErrors.RookoutError) { 81 return NewValueNamespace(reflect.ValueOf(g.Obj)).ReadAttribute(name) 82 } 83 84 func (g *GoObjectNamespace) WriteAttribute(_ string, _ Namespace) rookoutErrors.RookoutError { 85 return rookoutErrors.NewNotImplemented() 86 } 87 88 func (g *GoObjectNamespace) ReadComplexKey(key interface{}) Namespace { 89 if g.Obj == nil { 90 return nil 91 } 92 93 reflectedValue := reflect.ValueOf(g.Obj) 94 if reflectedValue.Kind() == reflect.Ptr { 95 reflectedValue = reflectedValue.Elem() 96 } 97 98 switch reflect.Zero(reflectedValue.Type()).String() { 99 case "<*list.List Value>": 100 l := reflectedValue.Interface().(*list.List) 101 102 return NewGoObjectNamespace(utils.GetElementInList(l, key.(int))) 103 } 104 105 return nil 106 } 107 108 func (g *GoObjectNamespace) ReadKey(key interface{}) (Namespace, rookoutErrors.RookoutError) { 109 if g.Obj == nil { 110 return nil, rookoutErrors.NewAgentKeyNotFoundException("", key, nil) 111 } 112 113 reflectedValue := reflect.ValueOf(g.Obj) 114 if reflectedValue.Kind() == reflect.Ptr { 115 reflectedValue = reflectedValue.Elem() 116 } 117 118 switch reflectedValue.Kind() { 119 case reflect.Array: 120 return NewGoObjectNamespace(reflectedValue.Index(key.(int)).Interface()), nil 121 122 case reflect.Map: 123 k := reflectedValue.MapKeys() 124 125 126 for i := 0; i < len(k); i++ { 127 if key.(string) == k[i].String() { 128 x := reflectedValue.MapIndex(k[i]).Interface() 129 return NewGoObjectNamespace(x), nil 130 } 131 } 132 133 case reflect.Struct: 134 return NewGoObjectNamespace(reflectedValue.FieldByName(key.(string))), nil 135 136 case reflect.Slice: 137 return NewGoObjectNamespace(reflectedValue.Index(key.(int)).Interface()), nil 138 139 default: 140 141 return g.ReadComplexKey(key), nil 142 } 143 return nil, rookoutErrors.NewAgentKeyNotFoundException("", key, nil) 144 } 145 146 func (g *GoObjectNamespace) GetObject() interface{} { 147 return g.Obj 148 } 149 150 func (g *GoObjectNamespace) Serialize(serializer Serializer) { 151 dumpInterface(serializer, g.Obj, g.ObjectDumpConf) 152 } 153 154 func (g *GoObjectNamespace) GetObjectDumpConfig() config.ObjectDumpConfig { 155 return g.ObjectDumpConf 156 } 157 158 func (g *GoObjectNamespace) SetObjectDumpConfig(config config.ObjectDumpConfig) { 159 g.ObjectDumpConf = config 160 }