Skip to content

Commit

Permalink
Rename methods in api/meta to be cleaner
Browse files Browse the repository at this point in the history
  • Loading branch information
smarterclayton committed Oct 23, 2014
1 parent 64d98cb commit 1ccb86c
Show file tree
Hide file tree
Showing 12 changed files with 77 additions and 77 deletions.
4 changes: 2 additions & 2 deletions cmd/kubecfg/kubecfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ func executeAPIRequest(ctx api.Context, method string, c *client.Client) bool {
if err != nil {
glog.Fatalf("error obtaining resource version for update: %v", err)
}
jsonBase, err := meta.FindTypeMeta(obj)
jsonBase, err := meta.FindAccessor(obj)
if err != nil {
glog.Fatalf("error finding json base for update: %v", err)
}
Expand Down Expand Up @@ -409,7 +409,7 @@ func executeAPIRequest(ctx api.Context, method string, c *client.Client) bool {
if err != nil {
glog.Fatalf("error setting resource version: %v", err)
}
jsonBase, err := meta.FindTypeMeta(obj)
jsonBase, err := meta.FindAccessor(obj)
if err != nil {
glog.Fatalf("error setting resource version: %v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/api/latest/latest.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ var Codec = v1beta1.Codec
// ResourceVersioner describes a default versioner that can handle all types
// of versioning.
// TODO: when versioning changes, make this part of each API definition.
var ResourceVersioner = meta.NewTypeMetaResourceVersioner()
var ResourceVersioner = meta.NewResourceVersioner()

// SelfLinker can set or get the SelfLink field of all API types.
// TODO: when versioning changes, make this part of each API definition.
// TODO(lavalamp): Combine SelfLinker & ResourceVersioner interfaces, force all uses
// to go through the InterfacesFor method below.
var SelfLinker = meta.NewTypeMetaSelfLinker()
var SelfLinker = meta.NewSelfLinker()

// VersionInterfaces contains the interfaces one should use for dealing with types of a particular version.
type VersionInterfaces struct {
Expand Down
72 changes: 36 additions & 36 deletions pkg/api/meta/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
)

// FindTypeMeta takes an arbitary api type, returns pointer to its TypeMeta field.
// FindAccessor takes an arbitary api type, returns pointer to its TypeMeta field.
// obj must be a pointer to an api type.
func FindTypeMeta(obj runtime.Object) (TypeMetaInterface, error) {
func FindAccessor(obj runtime.Object) (Accessor, error) {
v, err := conversion.EnforcePtr(obj)
if err != nil {
return nil, err
Expand All @@ -38,7 +38,7 @@ func FindTypeMeta(obj runtime.Object) (TypeMetaInterface, error) {
}
typeMeta := v.FieldByName("TypeMeta")
if !typeMeta.IsValid() {
return nil, fmt.Errorf("struct %v lacks embedded JSON type", name)
return nil, fmt.Errorf("struct %v lacks embedded TypeMeta type", name)
}
g, err := newGenericTypeMeta(typeMeta)
if err != nil {
Expand All @@ -47,67 +47,67 @@ func FindTypeMeta(obj runtime.Object) (TypeMetaInterface, error) {
return g, nil
}

// NewTypeMetaResourceVersioner returns a ResourceVersioner that can set or
// NewResourceVersioner returns a ResourceVersioner that can set or
// retrieve ResourceVersion on objects derived from TypeMeta.
func NewTypeMetaResourceVersioner() runtime.ResourceVersioner {
return jsonBaseModifier{}
func NewResourceVersioner() runtime.ResourceVersioner {
return typeMetaModifier{}
}

// jsonBaseModifier implements ResourceVersioner and SelfLinker.
type jsonBaseModifier struct{}
// typeMetaModifier implements ResourceVersioner and SelfLinker.
type typeMetaModifier struct{}

func (v jsonBaseModifier) ResourceVersion(obj runtime.Object) (string, error) {
json, err := FindTypeMeta(obj)
func (v typeMetaModifier) ResourceVersion(obj runtime.Object) (string, error) {
accessor, err := FindAccessor(obj)
if err != nil {
return "", err
}
return json.ResourceVersion(), nil
return accessor.ResourceVersion(), nil
}

func (v jsonBaseModifier) SetResourceVersion(obj runtime.Object, version string) error {
json, err := FindTypeMeta(obj)
func (v typeMetaModifier) SetResourceVersion(obj runtime.Object, version string) error {
accessor, err := FindAccessor(obj)
if err != nil {
return err
}
json.SetResourceVersion(version)
accessor.SetResourceVersion(version)
return nil
}

func (v jsonBaseModifier) ID(obj runtime.Object) (string, error) {
json, err := FindTypeMeta(obj)
func (v typeMetaModifier) Name(obj runtime.Object) (string, error) {
accessor, err := FindAccessor(obj)
if err != nil {
return "", err
}
return json.ID(), nil
return accessor.Name(), nil
}

func (v jsonBaseModifier) SelfLink(obj runtime.Object) (string, error) {
json, err := FindTypeMeta(obj)
func (v typeMetaModifier) SelfLink(obj runtime.Object) (string, error) {
accessor, err := FindAccessor(obj)
if err != nil {
return "", err
}
return json.SelfLink(), nil
return accessor.SelfLink(), nil
}

func (v jsonBaseModifier) SetSelfLink(obj runtime.Object, selfLink string) error {
json, err := FindTypeMeta(obj)
func (v typeMetaModifier) SetSelfLink(obj runtime.Object, selfLink string) error {
accessor, err := FindAccessor(obj)
if err != nil {
return err
}
json.SetSelfLink(selfLink)
accessor.SetSelfLink(selfLink)
return nil
}

// NewTypeMetaSelfLinker returns a SelfLinker that works on all TypeMeta SelfLink fields.
func NewTypeMetaSelfLinker() runtime.SelfLinker {
return jsonBaseModifier{}
// NewSelfLinker returns a SelfLinker that works on all TypeMeta SelfLink fields.
func NewSelfLinker() runtime.SelfLinker {
return typeMetaModifier{}
}

// TypeMetaInterface lets you work with a TypeMeta from any of the versioned or
// Accessor lets you work with object metadata from any of the versioned or
// internal APIruntime.Objects.
type TypeMetaInterface interface {
ID() string
SetID(ID string)
type Accessor interface {
Name() string
SetName(name string)
APIVersion() string
SetAPIVersion(version string)
Kind() string
Expand All @@ -119,19 +119,19 @@ type TypeMetaInterface interface {
}

type genericTypeMeta struct {
id *string
name *string
apiVersion *string
kind *string
resourceVersion *string
selfLink *string
}

func (g genericTypeMeta) ID() string {
return *g.id
func (g genericTypeMeta) Name() string {
return *g.name
}

func (g genericTypeMeta) SetID(id string) {
*g.id = id
func (g genericTypeMeta) SetName(name string) {
*g.name = name
}

func (g genericTypeMeta) APIVersion() string {
Expand Down Expand Up @@ -196,7 +196,7 @@ func fieldPtr(v reflect.Value, fieldName string, dest interface{}) error {
// Returns an error if this isn't the case.
func newGenericTypeMeta(v reflect.Value) (genericTypeMeta, error) {
g := genericTypeMeta{}
if err := fieldPtr(v, "Name", &g.id); err != nil {
if err := fieldPtr(v, "Name", &g.name); err != nil {
return g, err
}
if err := fieldPtr(v, "APIVersion", &g.apiVersion); err != nil {
Expand Down
12 changes: 6 additions & 6 deletions pkg/api/meta/meta_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ func TestGenericTypeMeta(t *testing.T) {
if err != nil {
t.Fatalf("new err: %v", err)
}
// Prove g supports TypeMetaInterface.
jbi := TypeMetaInterface(g)
if e, a := "foo", jbi.ID(); e != a {
// Prove g supports Accessor.
jbi := Accessor(g)
if e, a := "foo", jbi.Name(); e != a {
t.Errorf("expected %v, got %v", e, a)
}
if e, a := "a", jbi.APIVersion(); e != a {
Expand All @@ -62,7 +62,7 @@ func TestGenericTypeMeta(t *testing.T) {
t.Errorf("expected %v, got %v", e, a)
}

jbi.SetID("bar")
jbi.SetName("bar")
jbi.SetAPIVersion("c")
jbi.SetKind("d")
jbi.SetResourceVersion("2")
Expand Down Expand Up @@ -107,7 +107,7 @@ func TestResourceVersionerOfAPI(t *testing.T) {
"api object with version": {&MyAPIObject{TypeMeta: runtime.TypeMeta{ResourceVersion: "1"}}, "1"},
"pointer to api object with version": {&MyAPIObject{TypeMeta: runtime.TypeMeta{ResourceVersion: "1"}}, "1"},
}
versioning := NewTypeMetaResourceVersioner()
versioning := NewResourceVersioner()
for key, testCase := range testCases {
actual, err := versioning.ResourceVersion(testCase.Object)
if err != nil {
Expand Down Expand Up @@ -170,7 +170,7 @@ func TestTypeMetaSelfLinker(t *testing.T) {
},
}

linker := NewTypeMetaSelfLinker()
linker := NewSelfLinker()
for name, item := range table {
got, err := linker.SelfLink(item.obj)
if e, a := item.succeed, err == nil; e != a {
Expand Down
12 changes: 6 additions & 6 deletions pkg/api/ref.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,24 +36,24 @@ func GetReference(obj runtime.Object) (*ObjectReference, error) {
if obj == nil {
return nil, ErrNilObject
}
jsonBase, err := meta.FindTypeMeta(obj)
accessor, err := meta.FindAccessor(obj)
if err != nil {
return nil, err
}
_, kind, err := Scheme.ObjectVersionAndKind(obj)
if err != nil {
return nil, err
}
version := versionFromSelfLink.FindStringSubmatch(jsonBase.SelfLink())
version := versionFromSelfLink.FindStringSubmatch(accessor.SelfLink())
if len(version) < 2 {
return nil, fmt.Errorf("unexpected self link format: %v", jsonBase.SelfLink())
return nil, fmt.Errorf("unexpected self link format: %v", accessor.SelfLink())
}
return &ObjectReference{
Kind: kind,
APIVersion: version[1],
// TODO: correct Name and UID when TypeMeta makes a distinction
Name: jsonBase.ID(),
UID: jsonBase.ID(),
ResourceVersion: jsonBase.ResourceVersion(),
Name: accessor.Name(),
UID: accessor.Name(),
ResourceVersion: accessor.ResourceVersion(),
}, nil
}
4 changes: 2 additions & 2 deletions pkg/api/serialization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ var apiObjectFuzzer = fuzz.New().NilChance(.5).NumElements(1, 1).Funcs(
func runTest(t *testing.T, codec runtime.Codec, source runtime.Object) {
name := reflect.TypeOf(source).Elem().Name()
apiObjectFuzzer.Fuzz(source)
j, err := meta.FindTypeMeta(source)
j, err := meta.FindAccessor(source)
if err != nil {
t.Fatalf("Unexpected error %v for %#v", err, source)
}
Expand Down Expand Up @@ -160,7 +160,7 @@ func TestTypes(t *testing.T) {
t.Errorf("Couldn't make a %v? %v", kind, err)
continue
}
if _, err := meta.FindTypeMeta(item); err != nil {
if _, err := meta.FindAccessor(item); err != nil {
t.Logf("%s is not a TypeMeta and cannot be round tripped: %v", kind, err)
continue
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/apiserver/apiserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -564,11 +564,11 @@ func TestParseTimeout(t *testing.T) {
type setTestSelfLinker struct {
t *testing.T
expectedSet string
id string
name string
called bool
}

func (s *setTestSelfLinker) ID(runtime.Object) (string, error) { return s.id, nil }
func (s *setTestSelfLinker) Name(runtime.Object) (string, error) { return s.name, nil }
func (*setTestSelfLinker) SelfLink(runtime.Object) (string, error) { return "", nil }
func (s *setTestSelfLinker) SetSelfLink(obj runtime.Object, selfLink string) error {
if e, a := s.expectedSet, selfLink; e != a {
Expand All @@ -587,7 +587,7 @@ func TestSyncCreate(t *testing.T) {
}
selfLinker := &setTestSelfLinker{
t: t,
id: "bar",
name: "bar",
expectedSet: "/prefix/version/foo/bar",
}
handler := Handle(map[string]RESTStorage{
Expand Down Expand Up @@ -684,7 +684,7 @@ func TestAsyncCreateError(t *testing.T) {
}
selfLinker := &setTestSelfLinker{
t: t,
id: "bar",
name: "bar",
expectedSet: "/prefix/version/foo/bar",
}
handler := Handle(map[string]RESTStorage{"foo": &storage}, codec, "/prefix/version", selfLinker)
Expand Down
10 changes: 5 additions & 5 deletions pkg/apiserver/resthandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,14 @@ func (h *RESTHandler) setSelfLink(obj runtime.Object, req *http.Request) error {
return h.selfLinker.SetSelfLink(obj, newURL.String())
}

// Like setSelfLink, but appends the object's id.
func (h *RESTHandler) setSelfLinkAddID(obj runtime.Object, req *http.Request) error {
id, err := h.selfLinker.ID(obj)
// Like setSelfLink, but appends the object's name.
func (h *RESTHandler) setSelfLinkAddName(obj runtime.Object, req *http.Request) error {
name, err := h.selfLinker.Name(obj)
if err != nil {
return err
}
newURL := *req.URL
newURL.Path = path.Join(h.canonicalPrefix, req.URL.Path, id)
newURL.Path = path.Join(h.canonicalPrefix, req.URL.Path, name)
newURL.RawQuery = ""
newURL.Fragment = ""
return h.selfLinker.SetSelfLink(obj, newURL.String())
Expand Down Expand Up @@ -171,7 +171,7 @@ func (h *RESTHandler) handleRESTStorage(parts []string, req *http.Request, w htt
errorJSON(err, h.codec, w)
return
}
op := h.createOperation(out, sync, timeout, curry(h.setSelfLinkAddID, req))
op := h.createOperation(out, sync, timeout, curry(h.setSelfLinkAddName, req))
h.finishReq(op, req, w)

case "DELETE":
Expand Down
14 changes: 7 additions & 7 deletions pkg/client/cache/reflector.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func (r *Reflector) listAndWatch() {
glog.Errorf("Failed to list %v: %v", r.expectedType, err)
return
}
jsonBase, err := meta.FindTypeMeta(list)
jsonBase, err := meta.FindAccessor(list)
if err != nil {
glog.Errorf("Unable to understand list result %#v", list)
return
Expand Down Expand Up @@ -113,11 +113,11 @@ func (r *Reflector) listAndWatch() {
func (r *Reflector) syncWith(items []runtime.Object) error {
found := map[string]interface{}{}
for _, item := range items {
jsonBase, err := meta.FindTypeMeta(item)
jsonBase, err := meta.FindAccessor(item)
if err != nil {
return fmt.Errorf("unexpected item in list: %v", err)
}
found[jsonBase.ID()] = item
found[jsonBase.Name()] = item
}

r.store.Replace(found)
Expand All @@ -140,21 +140,21 @@ func (r *Reflector) watchHandler(w watch.Interface, resourceVersion *string) err
glog.Errorf("expected type %v, but watch event object had type %v", e, a)
continue
}
jsonBase, err := meta.FindTypeMeta(event.Object)
jsonBase, err := meta.FindAccessor(event.Object)
if err != nil {
glog.Errorf("unable to understand watch event %#v", event)
continue
}
switch event.Type {
case watch.Added:
r.store.Add(jsonBase.ID(), event.Object)
r.store.Add(jsonBase.Name(), event.Object)
case watch.Modified:
r.store.Update(jsonBase.ID(), event.Object)
r.store.Update(jsonBase.Name(), event.Object)
case watch.Deleted:
// TODO: Will any consumers need access to the "last known
// state", which is passed in event.Object? If so, may need
// to change this.
r.store.Delete(jsonBase.ID())
r.store.Delete(jsonBase.Name())
default:
glog.Errorf("unable to understand watch event %#v", event)
}
Expand Down
Loading

0 comments on commit 1ccb86c

Please sign in to comment.