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

     1  package namespaces
     2  
     3  import (
     4  	"io"
     5  
     6  	"github.com/Rookout/GoSDK/pkg/rookoutErrors"
     7  )
     8  
     9  type ContainerNamespace struct {
    10  	Obj     map[string]Namespace
    11  	OnClose func() error
    12  }
    13  
    14  func NewEmptyContainerNamespace() *ContainerNamespace {
    15  	c := &ContainerNamespace{
    16  		Obj: make(map[string]Namespace),
    17  	}
    18  
    19  	return c
    20  }
    21  
    22  func NewContainerNamespace(initObj *map[string]Namespace) *ContainerNamespace {
    23  	if nil == initObj {
    24  		initObj = &map[string]Namespace{}
    25  	}
    26  
    27  	c := &ContainerNamespace{
    28  		Obj: *initObj,
    29  	}
    30  
    31  	return c
    32  }
    33  
    34  func (c *ContainerNamespace) CallMethod(name string, _ string) (Namespace, rookoutErrors.RookoutError) {
    35  	switch name {
    36  	case "size":
    37  		return NewGoObjectNamespace(len(c.Obj)), nil
    38  
    39  	default:
    40  		return nil, rookoutErrors.NewRookMethodNotFound(name)
    41  	}
    42  }
    43  
    44  func (c *ContainerNamespace) ReadAttribute(name string) (Namespace, rookoutErrors.RookoutError) {
    45  	if val, ok := c.Obj[name]; ok {
    46  		return val, nil
    47  	}
    48  	return nil, rookoutErrors.NewRookAttributeNotFoundException(name)
    49  }
    50  
    51  func (c *ContainerNamespace) WriteAttribute(name string, value Namespace) rookoutErrors.RookoutError {
    52  	c.Obj[name] = value
    53  	return nil
    54  }
    55  
    56  func (c *ContainerNamespace) ReadKey(_ interface{}) (Namespace, rookoutErrors.RookoutError) {
    57  	return nil, rookoutErrors.NewNotImplemented()
    58  }
    59  
    60  func (c *ContainerNamespace) GetObject() interface{} {
    61  	return &c.Obj
    62  }
    63  
    64  func (c *ContainerNamespace) Serialize(serializer Serializer) {
    65  	names := make([]string, 0, len(c.Obj))
    66  	for k := range c.Obj {
    67  		names = append(names, k)
    68  	}
    69  
    70  	getNamedValue := func(i int) (string, Namespace) {
    71  		return names[i], c.Obj[names[i]]
    72  	}
    73  	serializer.dumpNamespace(getNamedValue, len(c.Obj))
    74  }
    75  
    76  func (c ContainerNamespace) Close() error {
    77  	var err error
    78  	if c.OnClose != nil {
    79  		err = c.OnClose()
    80  	}
    81  	for _, v := range c.Obj {
    82  		if closer, ok := v.(io.Closer); ok {
    83  			_ = closer.Close()
    84  		}
    85  	}
    86  	return err
    87  }