github.com/Rookout/GoSDK@v0.1.48/pkg/processor/namespaces/frame_namespace.go (about)

     1  package namespaces
     2  
     3  import (
     4  	"strconv"
     5  	"strings"
     6  
     7  	"github.com/Rookout/GoSDK/pkg/config"
     8  	"github.com/Rookout/GoSDK/pkg/rookoutErrors"
     9  	"github.com/Rookout/GoSDK/pkg/services/collection"
    10  	"github.com/Rookout/GoSDK/pkg/services/collection/variable"
    11  )
    12  
    13  type FrameNamespace struct {
    14  	collectionService *collection.CollectionService
    15  	locals            map[string]*VariableNamespace
    16  }
    17  
    18  func NewFrameNamespace(collectionService *collection.CollectionService) *FrameNamespace {
    19  	return &FrameNamespace{
    20  		collectionService: collectionService,
    21  		locals:            make(map[string]*VariableNamespace),
    22  	}
    23  }
    24  
    25  func (f *FrameNamespace) CallMethod(name string, args string) (Namespace, rookoutErrors.RookoutError) {
    26  	switch name {
    27  	case "filename":
    28  		return NewGoObjectNamespace(f.collectionService.GetFrame().File), nil
    29  	case "line":
    30  		return NewGoObjectNamespace(f.collectionService.GetFrame().Line), nil
    31  	case "method", "function":
    32  		return NewGoObjectNamespace(f.collectionService.GetFrame().Function), nil
    33  	case "locals":
    34  		return f.GetLocals(args)
    35  	case "dump":
    36  		return f.GetDump(args)
    37  	default:
    38  		return nil, rookoutErrors.NewNotImplemented()
    39  	}
    40  }
    41  
    42  func (f *FrameNamespace) getAllVariables(config config.ObjectDumpConfig) ([]*variable.Variable, rookoutErrors.RookoutError) {
    43  	return f.collectionService.GetVariables(config), nil
    44  }
    45  
    46  func (f *FrameNamespace) variablesToLocals(vars []*variable.Variable, config config.ObjectDumpConfig) {
    47  	for _, v := range vars {
    48  		if _, ok := f.locals[v.Name]; !ok {
    49  			obj := NewVariableNamespace(v.Name, v, f.collectionService)
    50  			obj.ObjectDumpConf = config
    51  			f.locals[v.Name] = obj
    52  		}
    53  	}
    54  }
    55  
    56  func (f *FrameNamespace) GetLocals(args string) (Namespace, rookoutErrors.RookoutError) {
    57  	maxDepth := 0
    58  	dumpConfig := config.GetDefaultDumpConfig()
    59  
    60  	if len(args) > 0 {
    61  		var err error
    62  		if maxDepth, err = strconv.Atoi(args); err == nil {
    63  			dumpConfig.MaxDepth = maxDepth
    64  		} else {
    65  			var ok bool
    66  			dumpConfig, ok = config.GetObjectDumpConfig(strings.ToLower(args))
    67  			if !ok {
    68  				return nil, rookoutErrors.NewRookInvalidMethodArguments("locals()", args)
    69  			}
    70  		}
    71  	}
    72  	vars, err := f.getAllVariables(dumpConfig)
    73  	if err != nil {
    74  		return nil, err
    75  	}
    76  
    77  	f.variablesToLocals(vars, dumpConfig)
    78  	locals := make(map[string]Namespace, len(f.locals))
    79  	for name, local := range f.locals {
    80  		locals[name] = local
    81  	}
    82  
    83  	return NewContainerNamespace(&locals), nil
    84  }
    85  
    86  func (f *FrameNamespace) GetDump(args string) (*ContainerNamespace, rookoutErrors.RookoutError) {
    87  	c := NewEmptyContainerNamespace()
    88  
    89  	locals, err := f.GetLocals(args)
    90  	if err != nil {
    91  		return nil, err
    92  	}
    93  	_ = c.WriteAttribute("locals", locals)
    94  	_ = c.WriteAttribute("filename", NewGoObjectNamespace(f.collectionService.GetFrame().File))
    95  	_ = c.WriteAttribute("module", NewGoObjectNamespace(f.collectionService.GetFrame().File))
    96  	_ = c.WriteAttribute("line", NewGoObjectNamespace(f.collectionService.GetFrame().Line))
    97  	_ = c.WriteAttribute("function", NewGoObjectNamespace(f.collectionService.GetFrame().Function))
    98  
    99  	return c, nil
   100  }
   101  
   102  func (f *FrameNamespace) ReadAttribute(name string) (Namespace, rookoutErrors.RookoutError) {
   103  	
   104  	if local, ok := f.locals[name]; ok {
   105  		if local.ObjectDumpConf.IsTailored {
   106  			return local, nil
   107  		}
   108  	}
   109  
   110  	dumpConfig := config.GetDefaultDumpConfig()
   111  	dumpConfig.ShouldTailor = true
   112  
   113  	
   114  	v, err := f.collectionService.GetVariable(name, dumpConfig)
   115  	if err != nil {
   116  		return nil, rookoutErrors.NewRookAttributeNotFoundException(name)
   117  	}
   118  	obj := NewVariableNamespace(name, v, f.collectionService)
   119  	f.locals[name] = obj
   120  	return obj, nil
   121  }
   122  
   123  func (f *FrameNamespace) WriteAttribute(_ string, _ Namespace) rookoutErrors.RookoutError {
   124  	return rookoutErrors.NewNotImplemented()
   125  }
   126  
   127  func (f *FrameNamespace) ReadKey(_ interface{}) (Namespace, rookoutErrors.RookoutError) {
   128  	return nil, rookoutErrors.NewNotImplemented()
   129  }
   130  
   131  func (f *FrameNamespace) GetObject() interface{} {
   132  	return nil
   133  }
   134  
   135  func (f *FrameNamespace) Serialize(serializer Serializer) {
   136  	dump, _ := f.GetDump("")
   137  	dump.Serialize(serializer)
   138  }