func newExternalScheme() (*runtime.Scheme, meta.RESTMapper, runtime.Codec) { scheme := runtime.NewScheme() scheme.AddKnownTypeWithName("", "Type", &internalType{}) scheme.AddKnownTypeWithName("unlikelyversion", "Type", &externalType{}) //This tests that qingctl will not confuse the external scheme with the internal scheme, even when they accidentally have versions of the same name. scheme.AddKnownTypeWithName(testapi.Version(), "Type", &ExternalType2{}) codec := runtime.CodecFor(scheme, "unlikelyversion") validVersion := testapi.Version() mapper := meta.NewDefaultRESTMapper([]string{"unlikelyversion", validVersion}, func(version string) (*meta.VersionInterfaces, bool) { return &meta.VersionInterfaces{ Codec: runtime.CodecFor(scheme, version), ObjectConvertor: scheme, MetadataAccessor: meta.NewAccessor(), }, (version == validVersion || version == "unlikelyversion") }) for _, version := range []string{"unlikelyversion", validVersion} { for kind := range scheme.KnownTypes(version) { mixedCase := false scope := meta.RESTScopeNamespace mapper.Add(scope, kind, version, mixedCase) } } return scheme, mapper, codec }
func init() { scheme = runtime.NewScheme() scheme.AddKnownTypes("", &TestResource{}) scheme.AddKnownTypes(testapi.Version(), &TestResource{}) codec = runtime.CodecFor(scheme, testapi.Version()) scheme.AddConversionFuncs( func(in *TestResource, out *TestResource, s conversion.Scope) error { *out = *in return nil }, ) }
func serviceErrorHandler(requestResolver *APIRequestInfoResolver, apiVersions []string, serviceErr restful.ServiceError, request *restful.Request, response *restful.Response) { requestInfo, err := requestResolver.GetAPIRequestInfo(request.Request) codec := latest.Codec if err == nil && requestInfo.APIVersion != "" { // check if the api version is valid. for _, version := range apiVersions { if requestInfo.APIVersion == version { // valid api version. codec = runtime.CodecFor(api.Scheme, requestInfo.APIVersion) break } } } errorJSON(apierrors.NewGenericServerResponse(serviceErr.Code, "", "", "", "", 0, false), codec, response.ResponseWriter) }
func TestEncode(t *testing.T) { scheme := runtime.NewScheme() scheme.AddKnownTypeWithName("", "Simple", &InternalSimple{}) scheme.AddKnownTypeWithName("externalVersion", "Simple", &ExternalSimple{}) codec := runtime.CodecFor(scheme, "externalVersion") test := &InternalSimple{ TestString: "I'm the same", } obj := runtime.Object(test) data, err := codec.Encode(obj) obj2, err2 := codec.Decode(data) if err != nil || err2 != nil { t.Fatalf("Failure: '%v' '%v'", err, err2) } if _, ok := obj2.(*InternalSimple); !ok { t.Fatalf("Got wrong type") } if !reflect.DeepEqual(obj2, test) { t.Errorf("Expected:\n %#v,\n Got:\n %#v", &test, obj2) } }
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ package v1 import ( "github.com/qingyuancloud/QingYuan/pkg/runtime" "github.com/qingyuancloud/QingYuan/plugin/pkg/scheduler/api" ) // Codec encodes internal objects to the v1 scheme var Codec = runtime.CodecFor(api.Scheme, "v1") func init() { api.Scheme.AddKnownTypes("v1", &Policy{}, ) } func (*Policy) IsAnAPIObject() {}
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ package v1beta3 import ( "github.com/qingyuancloud/QingYuan/pkg/api" "github.com/qingyuancloud/QingYuan/pkg/api/registered" "github.com/qingyuancloud/QingYuan/pkg/runtime" ) // Codec encodes internal objects to the v1beta3 scheme var Codec = runtime.CodecFor(api.Scheme, "v1beta3") func init() { // Check if v1beta3 is in the list of supported API versions. if !registered.IsRegisteredAPIVersion("v1beta3") { return } // Register the API. addKnownTypes() addConversionFuncs() addDefaultingFuncs() } // Adds the list of known types to api.Scheme. func addKnownTypes() {
*/ package api import ( "github.com/qingyuancloud/QingYuan/pkg/api/resource" "github.com/qingyuancloud/QingYuan/pkg/conversion" "github.com/qingyuancloud/QingYuan/pkg/fields" "github.com/qingyuancloud/QingYuan/pkg/labels" "github.com/qingyuancloud/QingYuan/pkg/runtime" "github.com/qingyuancloud/QingYuan/pkg/util" ) // Codec is the identity codec for this package - it can only convert itself // to itself. var Codec = runtime.CodecFor(Scheme, "") func init() { Scheme.AddDefaultingFuncs( func(obj *ListOptions) { obj.LabelSelector = labels.Everything() obj.FieldSelector = fields.Everything() }, func(obj *PodExecOptions) { obj.Stderr = true obj.Stdout = true }, ) Scheme.AddConversionFuncs( func(in *util.Time, out *util.Time, s conversion.Scope) error { // Cannot deep copy these, because time.Time has unexported fields.