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