github.com/spotmaxtech/k8s-apimachinery-v0260@v0.0.1/pkg/apis/meta/v1/group_version.go (about)

     1  /*
     2  Copyright 2015 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package v1
    18  
    19  import (
    20  	"encoding/json"
    21  	"fmt"
    22  	"strings"
    23  
    24  	"github.com/spotmaxtech/k8s-apimachinery-v0260/pkg/runtime/schema"
    25  )
    26  
    27  // GroupResource specifies a Group and a Resource, but does not force a version.  This is useful for identifying
    28  // concepts during lookup stages without having partially valid types
    29  //
    30  // +protobuf.options.(gogoproto.goproto_stringer)=false
    31  type GroupResource struct {
    32  	Group    string `json:"group" protobuf:"bytes,1,opt,name=group"`
    33  	Resource string `json:"resource" protobuf:"bytes,2,opt,name=resource"`
    34  }
    35  
    36  func (gr *GroupResource) String() string {
    37  	if gr == nil {
    38  		return "<nil>"
    39  	}
    40  	if len(gr.Group) == 0 {
    41  		return gr.Resource
    42  	}
    43  	return gr.Resource + "." + gr.Group
    44  }
    45  
    46  // GroupVersionResource unambiguously identifies a resource.  It doesn't anonymously include GroupVersion
    47  // to avoid automatic coercion.  It doesn't use a GroupVersion to avoid custom marshalling
    48  //
    49  // +protobuf.options.(gogoproto.goproto_stringer)=false
    50  type GroupVersionResource struct {
    51  	Group    string `json:"group" protobuf:"bytes,1,opt,name=group"`
    52  	Version  string `json:"version" protobuf:"bytes,2,opt,name=version"`
    53  	Resource string `json:"resource" protobuf:"bytes,3,opt,name=resource"`
    54  }
    55  
    56  func (gvr *GroupVersionResource) String() string {
    57  	if gvr == nil {
    58  		return "<nil>"
    59  	}
    60  	return strings.Join([]string{gvr.Group, "/", gvr.Version, ", Resource=", gvr.Resource}, "")
    61  }
    62  
    63  // GroupKind specifies a Group and a Kind, but does not force a version.  This is useful for identifying
    64  // concepts during lookup stages without having partially valid types
    65  //
    66  // +protobuf.options.(gogoproto.goproto_stringer)=false
    67  type GroupKind struct {
    68  	Group string `json:"group" protobuf:"bytes,1,opt,name=group"`
    69  	Kind  string `json:"kind" protobuf:"bytes,2,opt,name=kind"`
    70  }
    71  
    72  func (gk *GroupKind) String() string {
    73  	if gk == nil {
    74  		return "<nil>"
    75  	}
    76  	if len(gk.Group) == 0 {
    77  		return gk.Kind
    78  	}
    79  	return gk.Kind + "." + gk.Group
    80  }
    81  
    82  // GroupVersionKind unambiguously identifies a kind.  It doesn't anonymously include GroupVersion
    83  // to avoid automatic coercion.  It doesn't use a GroupVersion to avoid custom marshalling
    84  //
    85  // +protobuf.options.(gogoproto.goproto_stringer)=false
    86  type GroupVersionKind struct {
    87  	Group   string `json:"group" protobuf:"bytes,1,opt,name=group"`
    88  	Version string `json:"version" protobuf:"bytes,2,opt,name=version"`
    89  	Kind    string `json:"kind" protobuf:"bytes,3,opt,name=kind"`
    90  }
    91  
    92  func (gvk GroupVersionKind) String() string {
    93  	return gvk.Group + "/" + gvk.Version + ", Kind=" + gvk.Kind
    94  }
    95  
    96  // GroupVersion contains the "group" and the "version", which uniquely identifies the API.
    97  //
    98  // +protobuf.options.(gogoproto.goproto_stringer)=false
    99  type GroupVersion struct {
   100  	Group   string `json:"group" protobuf:"bytes,1,opt,name=group"`
   101  	Version string `json:"version" protobuf:"bytes,2,opt,name=version"`
   102  }
   103  
   104  // Empty returns true if group and version are empty
   105  func (gv GroupVersion) Empty() bool {
   106  	return len(gv.Group) == 0 && len(gv.Version) == 0
   107  }
   108  
   109  // String puts "group" and "version" into a single "group/version" string. For the legacy v1
   110  // it returns "v1".
   111  func (gv GroupVersion) String() string {
   112  	// special case the internal apiVersion for the legacy kube types
   113  	if gv.Empty() {
   114  		return ""
   115  	}
   116  
   117  	// special case of "v1" for backward compatibility
   118  	if len(gv.Group) == 0 && gv.Version == "v1" {
   119  		return gv.Version
   120  	}
   121  	if len(gv.Group) > 0 {
   122  		return gv.Group + "/" + gv.Version
   123  	}
   124  	return gv.Version
   125  }
   126  
   127  // MarshalJSON implements the json.Marshaller interface.
   128  func (gv GroupVersion) MarshalJSON() ([]byte, error) {
   129  	s := gv.String()
   130  	if strings.Count(s, "/") > 1 {
   131  		return []byte{}, fmt.Errorf("illegal GroupVersion %v: contains more than one /", s)
   132  	}
   133  	return json.Marshal(s)
   134  }
   135  
   136  func (gv *GroupVersion) unmarshal(value []byte) error {
   137  	var s string
   138  	if err := json.Unmarshal(value, &s); err != nil {
   139  		return err
   140  	}
   141  	parsed, err := schema.ParseGroupVersion(s)
   142  	if err != nil {
   143  		return err
   144  	}
   145  	gv.Group, gv.Version = parsed.Group, parsed.Version
   146  	return nil
   147  }
   148  
   149  // UnmarshalJSON implements the json.Unmarshaller interface.
   150  func (gv *GroupVersion) UnmarshalJSON(value []byte) error {
   151  	return gv.unmarshal(value)
   152  }
   153  
   154  // UnmarshalTEXT implements the Ugorji's encoding.TextUnmarshaler interface.
   155  func (gv *GroupVersion) UnmarshalText(value []byte) error {
   156  	return gv.unmarshal(value)
   157  }