// SetQingYuanDefaults sets default values on the provided client config for accessing the // QingYuan API or returns an error if any of the defaults are impossible or invalid. func SetQingYuanDefaults(config *Config) error { if config.Prefix == "" { config.Prefix = "/api" } if len(config.UserAgent) == 0 { config.UserAgent = DefaultQingYuanUserAgent() } if len(config.Version) == 0 { config.Version = defaultVersionFor(config) } version := config.Version versionInterfaces, err := latest.InterfacesFor(version) if err != nil { return fmt.Errorf("API version '%s' is not recognized (valid values: %s)", version, strings.Join(latest.Versions, ", ")) } if config.Codec == nil { config.Codec = versionInterfaces.Codec } if config.QPS == 0.0 { config.QPS = 5.0 } if config.Burst == 0 { config.Burst = 10 } return nil }
// MetadataAccessor returns the MetadataAccessor for the API version to test against, // as set by the QING_API_VERSION env var. func MetadataAccessor() meta.MetadataAccessor { interfaces, err := latest.InterfacesFor(Version()) if err != nil { panic(err) } return interfaces.MetadataAccessor }
// Converter returns the api.Scheme for the API version to test against, as set by the // QING_API_VERSION env var. func Converter() runtime.ObjectConvertor { interfaces, err := latest.InterfacesFor(Version()) if err != nil { panic(err) } return interfaces.ObjectConvertor }
// Codec returns the codec for the API version to test against, as set by the // QING_API_VERSION env var. func Codec() runtime.Codec { interfaces, err := latest.InterfacesFor(Version()) if err != nil { panic(err) } return interfaces.Codec }
// NewEtcdHelper returns an EtcdHelper for the provided arguments or an error if the version // is incorrect. func NewEtcdHelper(client tools.EtcdGetSet, version string, prefix string) (helper tools.EtcdHelper, err error) { if version == "" { version = latest.Version } versionInterfaces, err := latest.InterfacesFor(version) if err != nil { return helper, err } return tools.NewEtcdHelper(client, versionInterfaces.Codec, prefix), nil }
func Merge(dst runtime.Object, fragment, kind string) (runtime.Object, error) { // Ok, this is a little hairy, we'd rather not force the user to specify a kind for their JSON // So we pull it into a map, add the Kind field, and then reserialize. // We also pull the apiVersion for proper parsing var intermediate interface{} if err := json.Unmarshal([]byte(fragment), &intermediate); err != nil { return nil, err } dataMap, ok := intermediate.(map[string]interface{}) if !ok { return nil, fmt.Errorf("Expected a map, found something else: %s", fragment) } version, found := dataMap["apiVersion"] if !found { return nil, fmt.Errorf("Inline JSON requires an apiVersion field") } versionString, ok := version.(string) if !ok { return nil, fmt.Errorf("apiVersion must be a string") } i, err := latest.InterfacesFor(versionString) if err != nil { return nil, err } // encode dst into versioned json and apply fragment directly too it target, err := i.Codec.Encode(dst) if err != nil { return nil, err } patched, err := jsonpatch.MergePatch(target, []byte(fragment)) if err != nil { return nil, err } out, err := i.Codec.Decode(patched) if err != nil { return nil, err } return out, nil }