Skip to content

Commit

Permalink
engine: traits: Make encoded fields public
Browse files Browse the repository at this point in the history
The fields that we might want to store with encoding/gob or any other
encoding package, need to be public. We currently don't use any of these
at the moment, but we might in the future.
  • Loading branch information
purpleidea committed Mar 6, 2019
1 parent efef260 commit 8da0da0
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 26 deletions.
48 changes: 48 additions & 0 deletions engine/resources/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,12 @@ func TestMiscEncodeDecode2(t *testing.T) {
t.Errorf("Can't create: %v", err)
return
}
// NOTE: Do not add this bit of code, because it would cause the path to
// get taken from the actual Path parameter, instead of using the name,
// and if we use the name, the Cmp function will detect if the name is
// stored properly or not.
//fileRes := input.(*FileRes) // must not panic
//fileRes.Path = "/tmp/whatever"

b64, err := engineUtil.ResToB64(input)
if err != nil {
Expand All @@ -142,11 +148,53 @@ func TestMiscEncodeDecode2(t *testing.T) {
t.Errorf("Output %v is not a Res", res2)
return
}
// this uses the standalone file cmp function
if err := res1.Cmp(res2); err != nil {
t.Errorf("The input and output Res values do not match: %+v", err)
}
}

func TestMiscEncodeDecode3(t *testing.T) {
var err error

// encode
input, err := engine.NewNamedResource("file", "file1")
if err != nil {
t.Errorf("Can't create: %v", err)
return
}
fileRes := input.(*FileRes) // must not panic
fileRes.Path = "/tmp/whatever"
// TODO: add other params/traits/etc here!

b64, err := engineUtil.ResToB64(input)
if err != nil {
t.Errorf("Can't encode: %v", err)
return
}

output, err := engineUtil.B64ToRes(b64)
if err != nil {
t.Errorf("Can't decode: %v", err)
return
}

res1, ok := input.(engine.Res)
if !ok {
t.Errorf("Input %v is not a Res", res1)
return
}
res2, ok := output.(engine.Res)
if !ok {
t.Errorf("Output %v is not a Res", res2)
return
}
// this uses the more complete, engine cmp function
if err := engine.ResCmp(res1, res2); err != nil {
t.Errorf("The input and output Res values do not match: %+v", err)
}
}

func TestFileAbsolute1(t *testing.T) {
// file resource paths should be absolute
f1 := &FileRes{
Expand Down
12 changes: 7 additions & 5 deletions engine/traits/autoedge.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,26 @@ import (
// methods needed to support autoedges on resources. It may be used as a start
// point to avoid re-implementing the straightforward methods.
type Edgeable struct {
meta *engine.AutoEdgeMeta
// Xmeta is the stored meta. It should be called `meta` but it must be
// public so that the `encoding/gob` package can encode it properly.
Xmeta *engine.AutoEdgeMeta

// Bug5819 works around issue https://github.com/golang/go/issues/5819
Bug5819 interface{} // XXX: workaround
}

// AutoEdgeMeta lets you get or set meta params for the automatic edges trait.
func (obj *Edgeable) AutoEdgeMeta() *engine.AutoEdgeMeta {
if obj.meta == nil { // set the defaults if previously empty
obj.meta = &engine.AutoEdgeMeta{
if obj.Xmeta == nil { // set the defaults if previously empty
obj.Xmeta = &engine.AutoEdgeMeta{
Disabled: false,
}
}
return obj.meta
return obj.Xmeta
}

// SetAutoEdgeMeta lets you set all of the meta params for the automatic edges
// trait in a single call.
func (obj *Edgeable) SetAutoEdgeMeta(meta *engine.AutoEdgeMeta) {
obj.meta = meta
obj.Xmeta = meta
}
12 changes: 7 additions & 5 deletions engine/traits/autogroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ import (
// methods needed to support autogrouping on resources. It may be used as a
// starting point to avoid re-implementing the straightforward methods.
type Groupable struct {
meta *engine.AutoGroupMeta
// Xmeta is the stored meta. It should be called `meta` but it must be
// public so that the `encoding/gob` package can encode it properly.
Xmeta *engine.AutoGroupMeta

isGrouped bool // am i contained within a group?
grouped []engine.GroupableRes // list of any grouped resources
Expand All @@ -39,18 +41,18 @@ type Groupable struct {
// AutoGroupMeta lets you get or set meta params for the automatic grouping
// trait.
func (obj *Groupable) AutoGroupMeta() *engine.AutoGroupMeta {
if obj.meta == nil { // set the defaults if previously empty
obj.meta = &engine.AutoGroupMeta{
if obj.Xmeta == nil { // set the defaults if previously empty
obj.Xmeta = &engine.AutoGroupMeta{
Disabled: false,
}
}
return obj.meta
return obj.Xmeta
}

// SetAutoGroupMeta lets you set all of the meta params for the automatic
// grouping trait in a single call.
func (obj *Groupable) SetAutoGroupMeta(meta *engine.AutoGroupMeta) {
obj.meta = meta
obj.Xmeta = meta
}

// GroupCmp compares two resources and decides if they're suitable for grouping.
Expand Down
16 changes: 13 additions & 3 deletions engine/traits/kind.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,33 @@

package traits

import (
"encoding/gob"
)

func init() {
gob.Register(&Kinded{})
}

// Kinded contains a general implementation of the properties and methods needed
// to support the resource kind. It should be used as a starting point to avoid
// re-implementing the straightforward kind methods.
type Kinded struct {
kind string
// Xkind is the stored kind. It should be called `kind` but it must be
// public so that the `encoding/gob` package can encode it properly.
Xkind string

// Bug5819 works around issue https://github.com/golang/go/issues/5819
Bug5819 interface{} // XXX: workaround
}

// Kind returns the string representation for the kind this resource is.
func (obj *Kinded) Kind() string {
return obj.kind
return obj.Xkind
}

// SetKind sets the kind string for this resource. It must only be set by the
// engine.
func (obj *Kinded) SetKind(kind string) {
obj.kind = kind
obj.Xkind = kind
}
12 changes: 7 additions & 5 deletions engine/traits/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,24 @@ import (
// to support meta parameters. It should be used as a starting point to avoid
// re-implementing the straightforward meta methods.
type Meta struct {
meta *engine.MetaParams
// Xmeta is the stored meta. It should be called `meta` but it must be
// public so that the `encoding/gob` package can encode it properly.
Xmeta *engine.MetaParams

// Bug5819 works around issue https://github.com/golang/go/issues/5819
Bug5819 interface{} // XXX: workaround
}

// MetaParams lets you get or set meta params for this trait.
func (obj *Meta) MetaParams() *engine.MetaParams {
if obj.meta == nil { // set the defaults if previously empty
obj.meta = engine.DefaultMetaParams.Copy()
if obj.Xmeta == nil { // set the defaults if previously empty
obj.Xmeta = engine.DefaultMetaParams.Copy()
}
return obj.meta
return obj.Xmeta
}

// SetMetaParams lets you set all of the meta params for the resource in a
// single call.
func (obj *Meta) SetMetaParams(meta *engine.MetaParams) {
obj.meta = meta
obj.Xmeta = meta
}
8 changes: 5 additions & 3 deletions engine/traits/named.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ package traits
// to support named resources. It should be used as a starting point to avoid
// re-implementing the straightforward name methods.
type Named struct {
name string
// Xname is the stored name. It should be called `name` but it must be
// public so that the `encoding/gob` package can encode it properly.
Xname string

// Bug5819 works around issue https://github.com/golang/go/issues/5819
Bug5819 interface{} // XXX: workaround
Expand All @@ -30,11 +32,11 @@ type Named struct {
// Name returns the unique name this resource has. It is only unique within its
// own kind.
func (obj *Named) Name() string {
return obj.name
return obj.Xname
}

// SetName sets the unique name for this resource. It must only be unique within
// its own kind.
func (obj *Named) SetName(name string) {
obj.name = name
obj.Xname = name
}
12 changes: 7 additions & 5 deletions engine/traits/reverse.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,26 @@ import (
// methods needed to support reversing resources. It may be used as a starting
// point to avoid re-implementing the straightforward methods.
type Reversible struct {
meta *engine.ReversibleMeta
// Xmeta is the stored meta. It should be called `meta` but it must be
// public so that the `encoding/gob` package can encode it properly.
Xmeta *engine.ReversibleMeta

// Bug5819 works around issue https://github.com/golang/go/issues/5819
Bug5819 interface{} // XXX: workaround
}

// ReversibleMeta lets you get or set meta params for the reversing trait.
func (obj *Reversible) ReversibleMeta() *engine.ReversibleMeta {
if obj.meta == nil { // set the defaults if previously empty
obj.meta = &engine.ReversibleMeta{
if obj.Xmeta == nil { // set the defaults if previously empty
obj.Xmeta = &engine.ReversibleMeta{
Disabled: true, // by default we're disabled
}
}
return obj.meta
return obj.Xmeta
}

// SetReversibleMeta lets you set all of the meta params for the reversing trait
// in a single call.
func (obj *Reversible) SetReversibleMeta(meta *engine.ReversibleMeta) {
obj.meta = meta
obj.Xmeta = meta
}

0 comments on commit 8da0da0

Please sign in to comment.