Rename JSONBase -> TypeMeta in preparation for v1beta3

Will make subsequent refactor much easier
This commit is contained in:
Clayton Coleman
2014-10-07 11:12:16 -04:00
parent e294cef9d4
commit d3e51a0f24
69 changed files with 464 additions and 464 deletions

View File

@@ -17,7 +17,7 @@ limitations under the License.
// Package runtime includes helper functions for working with API objects
// that follow the kubernetes API object conventions, which are:
//
// 0. Your API objects have a common metadata struct member, JSONBase.
// 0. Your API objects have a common metadata struct member, TypeMeta.
// 1. Your code refers to an internal set of API objects.
// 2. In a separate package, you have an external set of API objects.
// 3. The external set is considered to be versioned, and no breaking

View File

@@ -28,13 +28,13 @@ var scheme = runtime.NewScheme()
var Codec = runtime.CodecFor(scheme, "v1test")
type EmbeddedTest struct {
runtime.JSONBase `yaml:",inline" json:",inline"`
runtime.TypeMeta `yaml:",inline" json:",inline"`
Object runtime.EmbeddedObject `yaml:"object,omitempty" json:"object,omitempty"`
EmptyObject runtime.EmbeddedObject `yaml:"emptyObject,omitempty" json:"emptyObject,omitempty"`
}
type EmbeddedTestExternal struct {
runtime.JSONBase `yaml:",inline" json:",inline"`
runtime.TypeMeta `yaml:",inline" json:",inline"`
Object runtime.RawExtension `yaml:"object,omitempty" json:"object,omitempty"`
EmptyObject runtime.RawExtension `yaml:"emptyObject,omitempty" json:"emptyObject,omitempty"`
}
@@ -48,10 +48,10 @@ func TestEmbeddedObject(t *testing.T) {
s.AddKnownTypeWithName("v1test", "EmbeddedTest", &EmbeddedTestExternal{})
outer := &EmbeddedTest{
JSONBase: runtime.JSONBase{ID: "outer"},
TypeMeta: runtime.TypeMeta{ID: "outer"},
Object: runtime.EmbeddedObject{
&EmbeddedTest{
JSONBase: runtime.JSONBase{ID: "inner"},
TypeMeta: runtime.TypeMeta{ID: "inner"},
},
},
}

View File

@@ -29,9 +29,9 @@ import (
func TestExtractList(t *testing.T) {
pl := &api.PodList{
Items: []api.Pod{
{JSONBase: api.JSONBase{ID: "1"}},
{JSONBase: api.JSONBase{ID: "2"}},
{JSONBase: api.JSONBase{ID: "3"}},
{TypeMeta: api.TypeMeta{ID: "1"}},
{TypeMeta: api.TypeMeta{ID: "2"}},
{TypeMeta: api.TypeMeta{ID: "3"}},
},
}
list, err := runtime.ExtractList(pl)
@@ -51,9 +51,9 @@ func TestExtractList(t *testing.T) {
func TestSetList(t *testing.T) {
pl := &api.PodList{}
list := []runtime.Object{
&api.Pod{JSONBase: api.JSONBase{ID: "1"}},
&api.Pod{JSONBase: api.JSONBase{ID: "2"}},
&api.Pod{JSONBase: api.JSONBase{ID: "3"}},
&api.Pod{TypeMeta: api.TypeMeta{ID: "1"}},
&api.Pod{TypeMeta: api.TypeMeta{ID: "2"}},
&api.Pod{TypeMeta: api.TypeMeta{ID: "3"}},
}
err := runtime.SetList(pl, list)
if err != nil {

View File

@@ -21,9 +21,9 @@ import (
"reflect"
)
// NewJSONBaseResourceVersioner returns a ResourceVersioner that can set or
// retrieve ResourceVersion on objects derived from JSONBase.
func NewJSONBaseResourceVersioner() ResourceVersioner {
// NewTypeMetaResourceVersioner returns a ResourceVersioner that can set or
// retrieve ResourceVersion on objects derived from TypeMeta.
func NewTypeMetaResourceVersioner() ResourceVersioner {
return jsonBaseModifier{}
}
@@ -31,7 +31,7 @@ func NewJSONBaseResourceVersioner() ResourceVersioner {
type jsonBaseModifier struct{}
func (v jsonBaseModifier) ResourceVersion(obj Object) (uint64, error) {
json, err := FindJSONBase(obj)
json, err := FindTypeMeta(obj)
if err != nil {
return 0, err
}
@@ -39,7 +39,7 @@ func (v jsonBaseModifier) ResourceVersion(obj Object) (uint64, error) {
}
func (v jsonBaseModifier) SetResourceVersion(obj Object, version uint64) error {
json, err := FindJSONBase(obj)
json, err := FindTypeMeta(obj)
if err != nil {
return err
}
@@ -48,7 +48,7 @@ func (v jsonBaseModifier) SetResourceVersion(obj Object, version uint64) error {
}
func (v jsonBaseModifier) ID(obj Object) (string, error) {
json, err := FindJSONBase(obj)
json, err := FindTypeMeta(obj)
if err != nil {
return "", err
}
@@ -56,7 +56,7 @@ func (v jsonBaseModifier) ID(obj Object) (string, error) {
}
func (v jsonBaseModifier) SelfLink(obj Object) (string, error) {
json, err := FindJSONBase(obj)
json, err := FindTypeMeta(obj)
if err != nil {
return "", err
}
@@ -64,7 +64,7 @@ func (v jsonBaseModifier) SelfLink(obj Object) (string, error) {
}
func (v jsonBaseModifier) SetSelfLink(obj Object, selfLink string) error {
json, err := FindJSONBase(obj)
json, err := FindTypeMeta(obj)
if err != nil {
return err
}
@@ -72,14 +72,14 @@ func (v jsonBaseModifier) SetSelfLink(obj Object, selfLink string) error {
return nil
}
// NewJSONBaseSelfLinker returns a SelfLinker that works on all JSONBase SelfLink fields.
func NewJSONBaseSelfLinker() SelfLinker {
// NewTypeMetaSelfLinker returns a SelfLinker that works on all TypeMeta SelfLink fields.
func NewTypeMetaSelfLinker() SelfLinker {
return jsonBaseModifier{}
}
// JSONBaseInterface lets you work with a JSONBase from any of the versioned or
// TypeMetaInterface lets you work with a TypeMeta from any of the versioned or
// internal APIObjects.
type JSONBaseInterface interface {
type TypeMetaInterface interface {
ID() string
SetID(ID string)
APIVersion() string
@@ -92,7 +92,7 @@ type JSONBaseInterface interface {
SetSelfLink(selfLink string)
}
type genericJSONBase struct {
type genericTypeMeta struct {
id *string
apiVersion *string
kind *string
@@ -100,43 +100,43 @@ type genericJSONBase struct {
selfLink *string
}
func (g genericJSONBase) ID() string {
func (g genericTypeMeta) ID() string {
return *g.id
}
func (g genericJSONBase) SetID(id string) {
func (g genericTypeMeta) SetID(id string) {
*g.id = id
}
func (g genericJSONBase) APIVersion() string {
func (g genericTypeMeta) APIVersion() string {
return *g.apiVersion
}
func (g genericJSONBase) SetAPIVersion(version string) {
func (g genericTypeMeta) SetAPIVersion(version string) {
*g.apiVersion = version
}
func (g genericJSONBase) Kind() string {
func (g genericTypeMeta) Kind() string {
return *g.kind
}
func (g genericJSONBase) SetKind(kind string) {
func (g genericTypeMeta) SetKind(kind string) {
*g.kind = kind
}
func (g genericJSONBase) ResourceVersion() uint64 {
func (g genericTypeMeta) ResourceVersion() uint64 {
return *g.resourceVersion
}
func (g genericJSONBase) SetResourceVersion(version uint64) {
func (g genericTypeMeta) SetResourceVersion(version uint64) {
*g.resourceVersion = version
}
func (g genericJSONBase) SelfLink() string {
func (g genericTypeMeta) SelfLink() string {
return *g.selfLink
}
func (g genericJSONBase) SetSelfLink(selfLink string) {
func (g genericTypeMeta) SetSelfLink(selfLink string) {
*g.selfLink = selfLink
}
@@ -165,11 +165,11 @@ func fieldPtr(v reflect.Value, fieldName string, dest interface{}) error {
return fmt.Errorf("Couldn't assign/convert %v to %v", field.Type(), v.Type())
}
// newGenericJSONBase creates a new generic JSONBase from v, which must be an
// addressable/setable reflect.Value having the same fields as api.JSONBase.
// newGenericTypeMeta creates a new generic TypeMeta from v, which must be an
// addressable/setable reflect.Value having the same fields as api.TypeMeta.
// Returns an error if this isn't the case.
func newGenericJSONBase(v reflect.Value) (genericJSONBase, error) {
g := genericJSONBase{}
func newGenericTypeMeta(v reflect.Value) (genericTypeMeta, error) {
g := genericTypeMeta{}
if err := fieldPtr(v, "ID", &g.id); err != nil {
return g, err
}

View File

@@ -23,8 +23,8 @@ import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
)
func TestGenericJSONBase(t *testing.T) {
type JSONBase struct {
func TestGenericTypeMeta(t *testing.T) {
type TypeMeta struct {
Kind string `json:"kind,omitempty" yaml:"kind,omitempty"`
ID string `json:"id,omitempty" yaml:"id,omitempty"`
CreationTimestamp util.Time `json:"creationTimestamp,omitempty" yaml:"creationTimestamp,omitempty"`
@@ -32,19 +32,19 @@ func TestGenericJSONBase(t *testing.T) {
ResourceVersion uint64 `json:"resourceVersion,omitempty" yaml:"resourceVersion,omitempty"`
APIVersion string `json:"apiVersion,omitempty" yaml:"apiVersion,omitempty"`
}
j := JSONBase{
j := TypeMeta{
ID: "foo",
APIVersion: "a",
Kind: "b",
ResourceVersion: 1,
SelfLink: "some/place/only/we/know",
}
g, err := newGenericJSONBase(reflect.ValueOf(&j).Elem())
g, err := newGenericTypeMeta(reflect.ValueOf(&j).Elem())
if err != nil {
t.Fatalf("new err: %v", err)
}
// Prove g supports JSONBaseInterface.
jbi := JSONBaseInterface(g)
// Prove g supports TypeMetaInterface.
jbi := TypeMetaInterface(g)
if e, a := "foo", jbi.ID(); e != a {
t.Errorf("expected %v, got %v", e, a)
}
@@ -86,7 +86,7 @@ func TestGenericJSONBase(t *testing.T) {
}
type MyAPIObject struct {
JSONBase `yaml:",inline" json:",inline"`
TypeMeta `yaml:",inline" json:",inline"`
}
func (*MyAPIObject) IsAnAPIObject() {}
@@ -103,10 +103,10 @@ func TestResourceVersionerOfAPI(t *testing.T) {
}
testCases := map[string]T{
"empty api object": {&MyAPIObject{}, 0},
"api object with version": {&MyAPIObject{JSONBase: JSONBase{ResourceVersion: 1}}, 1},
"pointer to api object with version": {&MyAPIObject{JSONBase: JSONBase{ResourceVersion: 1}}, 1},
"api object with version": {&MyAPIObject{TypeMeta: TypeMeta{ResourceVersion: 1}}, 1},
"pointer to api object with version": {&MyAPIObject{TypeMeta: TypeMeta{ResourceVersion: 1}}, 1},
}
versioning := NewJSONBaseResourceVersioner()
versioning := NewTypeMetaResourceVersioner()
for key, testCase := range testCases {
actual, err := versioning.ResourceVersion(testCase.Object)
if err != nil {
@@ -134,7 +134,7 @@ func TestResourceVersionerOfAPI(t *testing.T) {
Object
Expected uint64
}{
"pointer to api object with version": {&MyAPIObject{JSONBase: JSONBase{ResourceVersion: 1}}, 1},
"pointer to api object with version": {&MyAPIObject{TypeMeta: TypeMeta{ResourceVersion: 1}}, 1},
}
for key, testCase := range setCases {
if err := versioning.SetResourceVersion(testCase.Object, 5); err != nil {
@@ -150,7 +150,7 @@ func TestResourceVersionerOfAPI(t *testing.T) {
}
}
func TestJSONBaseSelfLinker(t *testing.T) {
func TestTypeMetaSelfLinker(t *testing.T) {
table := map[string]struct {
obj Object
expect string
@@ -158,7 +158,7 @@ func TestJSONBaseSelfLinker(t *testing.T) {
succeed bool
}{
"normal": {
obj: &MyAPIObject{JSONBase: JSONBase{SelfLink: "foobar"}},
obj: &MyAPIObject{TypeMeta: TypeMeta{SelfLink: "foobar"}},
expect: "foobar",
try: "newbar",
succeed: true,
@@ -169,7 +169,7 @@ func TestJSONBaseSelfLinker(t *testing.T) {
},
}
linker := NewJSONBaseSelfLinker()
linker := NewTypeMetaSelfLinker()
for name, item := range table {
got, err := linker.SelfLink(item.obj)
if e, a := item.succeed, err == nil; e != a {

View File

@@ -247,9 +247,9 @@ func (s *Scheme) Convert(in, out interface{}) error {
return s.raw.Convert(in, out)
}
// FindJSONBase takes an arbitary api type, returns pointer to its JSONBase field.
// FindTypeMeta takes an arbitary api type, returns pointer to its TypeMeta field.
// obj must be a pointer to an api type.
func FindJSONBase(obj Object) (JSONBaseInterface, error) {
func FindTypeMeta(obj Object) (TypeMetaInterface, error) {
v, err := enforcePtr(obj)
if err != nil {
return nil, err
@@ -259,11 +259,11 @@ func FindJSONBase(obj Object) (JSONBaseInterface, error) {
if v.Kind() != reflect.Struct {
return nil, fmt.Errorf("expected struct, but got %v: %v (%#v)", v.Kind(), name, v.Interface())
}
jsonBase := v.FieldByName("JSONBase")
jsonBase := v.FieldByName("TypeMeta")
if !jsonBase.IsValid() {
return nil, fmt.Errorf("struct %v lacks embedded JSON type", name)
}
g, err := newGenericJSONBase(jsonBase)
g, err := newGenericTypeMeta(jsonBase)
if err != nil {
return nil, err
}
@@ -271,7 +271,7 @@ func FindJSONBase(obj Object) (JSONBaseInterface, error) {
}
// EncodeToVersion turns the given api object into an appropriate JSON string.
// Will return an error if the object doesn't have an embedded JSONBase.
// Will return an error if the object doesn't have an embedded TypeMeta.
// Obj may be a pointer to a struct, or a struct. If a struct, a copy
// must be made. If a pointer, the object may be modified before encoding,
// but will be put back into its original state before returning.
@@ -390,9 +390,9 @@ func ObjectDiff(a, b Object) string {
// metaInsertion implements conversion.MetaInsertionFactory, which lets the conversion
// package figure out how to encode our object's types and versions. These fields are
// located in our JSONBase.
// located in our TypeMeta.
type metaInsertion struct {
JSONBase struct {
TypeMeta struct {
APIVersion string `json:"apiVersion,omitempty" yaml:"apiVersion,omitempty"`
Kind string `json:"kind,omitempty" yaml:"kind,omitempty"`
} `json:",inline" yaml:",inline"`
@@ -401,8 +401,8 @@ type metaInsertion struct {
// Create returns a new metaInsertion with the version and kind fields set.
func (metaInsertion) Create(version, kind string) interface{} {
m := metaInsertion{}
m.JSONBase.APIVersion = version
m.JSONBase.Kind = kind
m.TypeMeta.APIVersion = version
m.TypeMeta.Kind = kind
return &m
}
@@ -410,5 +410,5 @@ func (metaInsertion) Create(version, kind string) interface{} {
// a metaInsertion pointer object.
func (metaInsertion) Interpret(in interface{}) (version, kind string) {
m := in.(*metaInsertion)
return m.JSONBase.APIVersion, m.JSONBase.Kind
return m.TypeMeta.APIVersion, m.TypeMeta.Kind
}

View File

@@ -24,18 +24,18 @@ import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
)
type JSONBase struct {
type TypeMeta struct {
Kind string `json:"kind,omitempty" yaml:"kind,omitempty"`
APIVersion string `json:"apiVersion,omitempty" yaml:"apiVersion,omitempty"`
}
type InternalSimple struct {
JSONBase `json:",inline" yaml:",inline"`
TypeMeta `json:",inline" yaml:",inline"`
TestString string `json:"testString" yaml:"testString"`
}
type ExternalSimple struct {
JSONBase `json:",inline" yaml:",inline"`
TypeMeta `json:",inline" yaml:",inline"`
TestString string `json:"testString" yaml:"testString"`
}
@@ -59,7 +59,7 @@ func TestScheme(t *testing.T) {
if e, a := "externalVersion", scope.Meta().DestVersion; e != a {
t.Errorf("Expected '%v', got '%v'", e, a)
}
scope.Convert(&in.JSONBase, &out.JSONBase, 0)
scope.Convert(&in.TypeMeta, &out.TypeMeta, 0)
scope.Convert(&in.TestString, &out.TestString, 0)
internalToExternalCalls++
return nil
@@ -71,7 +71,7 @@ func TestScheme(t *testing.T) {
if e, a := "", scope.Meta().DestVersion; e != a {
t.Errorf("Expected '%v', got '%v'", e, a)
}
scope.Convert(&in.JSONBase, &out.JSONBase, 0)
scope.Convert(&in.TypeMeta, &out.TypeMeta, 0)
scope.Convert(&in.TestString, &out.TestString, 0)
externalToInternalCalls++
return nil
@@ -150,12 +150,12 @@ type ExtensionB struct {
}
type ExternalExtensionType struct {
JSONBase `json:",inline" yaml:",inline"`
TypeMeta `json:",inline" yaml:",inline"`
Extension runtime.RawExtension `json:"extension" yaml:"extension"`
}
type InternalExtensionType struct {
JSONBase `json:",inline" yaml:",inline"`
TypeMeta `json:",inline" yaml:",inline"`
Extension runtime.EmbeddedObject `json:"extension" yaml:"extension"`
}

View File

@@ -23,18 +23,18 @@ import (
// Note that the types provided in this file are not versioned and are intended to be
// safe to use from within all versions of every API object.
// JSONBase is shared by all top level objects. The proper way to use it is to inline it in your type,
// TypeMeta is shared by all top level objects. The proper way to use it is to inline it in your type,
// like this:
// type MyAwesomeAPIObject struct {
// runtime.JSONBase `yaml:",inline" json:",inline"`
// runtime.TypeMeta `yaml:",inline" json:",inline"`
// ... // other fields
// }
// func (*MyAwesomeAPIObject) IsAnAPIObject() {}
//
// JSONBase is provided here for convenience. You may use it directly from this package or define
// TypeMeta is provided here for convenience. You may use it directly from this package or define
// your own with the same fields.
//
type JSONBase struct {
type TypeMeta struct {
Kind string `json:"kind,omitempty" yaml:"kind,omitempty"`
ID string `json:"id,omitempty" yaml:"id,omitempty"`
CreationTimestamp util.Time `json:"creationTimestamp,omitempty" yaml:"creationTimestamp,omitempty"`
@@ -43,7 +43,7 @@ type JSONBase struct {
APIVersion string `json:"apiVersion,omitempty" yaml:"apiVersion,omitempty"`
}
// PluginBase is like JSONBase, but it's intended for plugin objects that won't ever be encoded
// PluginBase is like TypeMeta, but it's intended for plugin objects that won't ever be encoded
// except while embedded in other objects.
type PluginBase struct {
Kind string `json:"kind,omitempty" yaml:"kind,omitempty"`
@@ -71,7 +71,7 @@ type EmbeddedObject struct {
//
// // Internal package:
// type MyAPIObject struct {
// runtime.JSONBase `yaml:",inline" json:",inline"`
// runtime.TypeMeta `yaml:",inline" json:",inline"`
// MyPlugin runtime.EmbeddedObject `json:"myPlugin" yaml:"myPlugin"`
// }
// type PluginA struct {
@@ -81,7 +81,7 @@ type EmbeddedObject struct {
//
// // External package:
// type MyAPIObject struct {
// runtime.JSONBase `yaml:",inline" json:",inline"`
// runtime.TypeMeta `yaml:",inline" json:",inline"`
// MyPlugin runtime.RawExtension `json:"myPlugin" yaml:"myPlugin"`
// }
// type PluginA struct {
@@ -112,10 +112,10 @@ type RawExtension struct {
// Unknown allows api objects with unknown types to be passed-through. This can be used
// to deal with the API objects from a plug-in. Unknown objects still have functioning
// JSONBase features-- kind, version, resourceVersion, etc.
// TypeMeta features-- kind, version, resourceVersion, etc.
// TODO: Not implemented yet!
type Unknown struct {
JSONBase `yaml:",inline" json:",inline"`
TypeMeta `yaml:",inline" json:",inline"`
// RawJSON will hold the complete JSON of the object which couldn't be matched
// with a registered type. Most likely, nothing should be done with this
// except for passing it through the system.