github.com/ali-iotechsys/cli@v20.10.0+incompatible/cli/context/store/storeconfig.go (about)

     1  package store
     2  
     3  // TypeGetter is a func used to determine the concrete type of a context or
     4  // endpoint metadata by returning a pointer to an instance of the object
     5  // eg: for a context of type DockerContext, the corresponding TypeGetter should return new(DockerContext)
     6  type TypeGetter func() interface{}
     7  
     8  // NamedTypeGetter is a TypeGetter associated with a name
     9  type NamedTypeGetter struct {
    10  	name       string
    11  	typeGetter TypeGetter
    12  }
    13  
    14  // EndpointTypeGetter returns a NamedTypeGetter with the spcecified name and getter
    15  func EndpointTypeGetter(name string, getter TypeGetter) NamedTypeGetter {
    16  	return NamedTypeGetter{
    17  		name:       name,
    18  		typeGetter: getter,
    19  	}
    20  }
    21  
    22  // Config is used to configure the metadata marshaler of the context store
    23  type Config struct {
    24  	contextType   TypeGetter
    25  	endpointTypes map[string]TypeGetter
    26  }
    27  
    28  // SetEndpoint set an endpoint typing information
    29  func (c Config) SetEndpoint(name string, getter TypeGetter) {
    30  	c.endpointTypes[name] = getter
    31  }
    32  
    33  // ForeachEndpointType calls cb on every endpoint type registered with the Config
    34  func (c Config) ForeachEndpointType(cb func(string, TypeGetter) error) error {
    35  	for n, ep := range c.endpointTypes {
    36  		if err := cb(n, ep); err != nil {
    37  			return err
    38  		}
    39  	}
    40  	return nil
    41  }
    42  
    43  // NewConfig creates a config object
    44  func NewConfig(contextType TypeGetter, endpoints ...NamedTypeGetter) Config {
    45  	res := Config{
    46  		contextType:   contextType,
    47  		endpointTypes: make(map[string]TypeGetter),
    48  	}
    49  	for _, e := range endpoints {
    50  		res.endpointTypes[e.name] = e.typeGetter
    51  	}
    52  	return res
    53  }