engine: traits: Make encoded fields public

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.
This commit is contained in:
James Shubin
2019-03-06 10:06:01 -05:00
parent efef260764
commit 8da0da02d9
7 changed files with 94 additions and 26 deletions

View File

@@ -119,6 +119,12 @@ func TestMiscEncodeDecode2(t *testing.T) {
t.Errorf("Can't create: %v", err) t.Errorf("Can't create: %v", err)
return 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) b64, err := engineUtil.ResToB64(input)
if err != nil { if err != nil {
@@ -142,11 +148,53 @@ func TestMiscEncodeDecode2(t *testing.T) {
t.Errorf("Output %v is not a Res", res2) t.Errorf("Output %v is not a Res", res2)
return return
} }
// this uses the standalone file cmp function
if err := res1.Cmp(res2); err != nil { if err := res1.Cmp(res2); err != nil {
t.Errorf("The input and output Res values do not match: %+v", err) 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) { func TestFileAbsolute1(t *testing.T) {
// file resource paths should be absolute // file resource paths should be absolute
f1 := &FileRes{ f1 := &FileRes{

View File

@@ -25,7 +25,9 @@ import (
// methods needed to support autoedges on resources. It may be used as a start // methods needed to support autoedges on resources. It may be used as a start
// point to avoid re-implementing the straightforward methods. // point to avoid re-implementing the straightforward methods.
type Edgeable struct { 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 works around issue https://github.com/golang/go/issues/5819
Bug5819 interface{} // XXX: workaround Bug5819 interface{} // XXX: workaround
@@ -33,16 +35,16 @@ type Edgeable struct {
// AutoEdgeMeta lets you get or set meta params for the automatic edges trait. // AutoEdgeMeta lets you get or set meta params for the automatic edges trait.
func (obj *Edgeable) AutoEdgeMeta() *engine.AutoEdgeMeta { func (obj *Edgeable) AutoEdgeMeta() *engine.AutoEdgeMeta {
if obj.meta == nil { // set the defaults if previously empty if obj.Xmeta == nil { // set the defaults if previously empty
obj.meta = &engine.AutoEdgeMeta{ obj.Xmeta = &engine.AutoEdgeMeta{
Disabled: false, Disabled: false,
} }
} }
return obj.meta return obj.Xmeta
} }
// SetAutoEdgeMeta lets you set all of the meta params for the automatic edges // SetAutoEdgeMeta lets you set all of the meta params for the automatic edges
// trait in a single call. // trait in a single call.
func (obj *Edgeable) SetAutoEdgeMeta(meta *engine.AutoEdgeMeta) { func (obj *Edgeable) SetAutoEdgeMeta(meta *engine.AutoEdgeMeta) {
obj.meta = meta obj.Xmeta = meta
} }

View File

@@ -27,7 +27,9 @@ import (
// methods needed to support autogrouping on resources. It may be used as a // methods needed to support autogrouping on resources. It may be used as a
// starting point to avoid re-implementing the straightforward methods. // starting point to avoid re-implementing the straightforward methods.
type Groupable struct { 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? isGrouped bool // am i contained within a group?
grouped []engine.GroupableRes // list of any grouped resources grouped []engine.GroupableRes // list of any grouped resources
@@ -39,18 +41,18 @@ type Groupable struct {
// AutoGroupMeta lets you get or set meta params for the automatic grouping // AutoGroupMeta lets you get or set meta params for the automatic grouping
// trait. // trait.
func (obj *Groupable) AutoGroupMeta() *engine.AutoGroupMeta { func (obj *Groupable) AutoGroupMeta() *engine.AutoGroupMeta {
if obj.meta == nil { // set the defaults if previously empty if obj.Xmeta == nil { // set the defaults if previously empty
obj.meta = &engine.AutoGroupMeta{ obj.Xmeta = &engine.AutoGroupMeta{
Disabled: false, Disabled: false,
} }
} }
return obj.meta return obj.Xmeta
} }
// SetAutoGroupMeta lets you set all of the meta params for the automatic // SetAutoGroupMeta lets you set all of the meta params for the automatic
// grouping trait in a single call. // grouping trait in a single call.
func (obj *Groupable) SetAutoGroupMeta(meta *engine.AutoGroupMeta) { 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. // GroupCmp compares two resources and decides if they're suitable for grouping.

View File

@@ -17,11 +17,21 @@
package traits package traits
import (
"encoding/gob"
)
func init() {
gob.Register(&Kinded{})
}
// Kinded contains a general implementation of the properties and methods needed // 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 // to support the resource kind. It should be used as a starting point to avoid
// re-implementing the straightforward kind methods. // re-implementing the straightforward kind methods.
type Kinded struct { 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 works around issue https://github.com/golang/go/issues/5819
Bug5819 interface{} // XXX: workaround Bug5819 interface{} // XXX: workaround
@@ -29,11 +39,11 @@ type Kinded struct {
// Kind returns the string representation for the kind this resource is. // Kind returns the string representation for the kind this resource is.
func (obj *Kinded) Kind() string { 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 // SetKind sets the kind string for this resource. It must only be set by the
// engine. // engine.
func (obj *Kinded) SetKind(kind string) { func (obj *Kinded) SetKind(kind string) {
obj.kind = kind obj.Xkind = kind
} }

View File

@@ -25,7 +25,9 @@ import (
// to support meta parameters. It should be used as a starting point to avoid // to support meta parameters. It should be used as a starting point to avoid
// re-implementing the straightforward meta methods. // re-implementing the straightforward meta methods.
type Meta struct { 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 works around issue https://github.com/golang/go/issues/5819
Bug5819 interface{} // XXX: workaround Bug5819 interface{} // XXX: workaround
@@ -33,14 +35,14 @@ type Meta struct {
// MetaParams lets you get or set meta params for this trait. // MetaParams lets you get or set meta params for this trait.
func (obj *Meta) MetaParams() *engine.MetaParams { func (obj *Meta) MetaParams() *engine.MetaParams {
if obj.meta == nil { // set the defaults if previously empty if obj.Xmeta == nil { // set the defaults if previously empty
obj.meta = engine.DefaultMetaParams.Copy() 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 // SetMetaParams lets you set all of the meta params for the resource in a
// single call. // single call.
func (obj *Meta) SetMetaParams(meta *engine.MetaParams) { func (obj *Meta) SetMetaParams(meta *engine.MetaParams) {
obj.meta = meta obj.Xmeta = meta
} }

View File

@@ -21,7 +21,9 @@ package traits
// to support named resources. It should be used as a starting point to avoid // to support named resources. It should be used as a starting point to avoid
// re-implementing the straightforward name methods. // re-implementing the straightforward name methods.
type Named struct { 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 works around issue https://github.com/golang/go/issues/5819
Bug5819 interface{} // XXX: workaround Bug5819 interface{} // XXX: workaround
@@ -30,11 +32,11 @@ type Named struct {
// Name returns the unique name this resource has. It is only unique within its // Name returns the unique name this resource has. It is only unique within its
// own kind. // own kind.
func (obj *Named) Name() string { 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 // SetName sets the unique name for this resource. It must only be unique within
// its own kind. // its own kind.
func (obj *Named) SetName(name string) { func (obj *Named) SetName(name string) {
obj.name = name obj.Xname = name
} }

View File

@@ -25,7 +25,9 @@ import (
// methods needed to support reversing resources. It may be used as a starting // methods needed to support reversing resources. It may be used as a starting
// point to avoid re-implementing the straightforward methods. // point to avoid re-implementing the straightforward methods.
type Reversible struct { 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 works around issue https://github.com/golang/go/issues/5819
Bug5819 interface{} // XXX: workaround Bug5819 interface{} // XXX: workaround
@@ -33,16 +35,16 @@ type Reversible struct {
// ReversibleMeta lets you get or set meta params for the reversing trait. // ReversibleMeta lets you get or set meta params for the reversing trait.
func (obj *Reversible) ReversibleMeta() *engine.ReversibleMeta { func (obj *Reversible) ReversibleMeta() *engine.ReversibleMeta {
if obj.meta == nil { // set the defaults if previously empty if obj.Xmeta == nil { // set the defaults if previously empty
obj.meta = &engine.ReversibleMeta{ obj.Xmeta = &engine.ReversibleMeta{
Disabled: true, // by default we're disabled 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 // SetReversibleMeta lets you set all of the meta params for the reversing trait
// in a single call. // in a single call.
func (obj *Reversible) SetReversibleMeta(meta *engine.ReversibleMeta) { func (obj *Reversible) SetReversibleMeta(meta *engine.ReversibleMeta) {
obj.meta = meta obj.Xmeta = meta
} }