Add caps.Type.MarshalJSON()

Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
This commit is contained in:
Zygmunt Krynicki
2015-11-24 12:47:28 +01:00
parent fb42b48bb9
commit a14c5e204a
2 changed files with 13 additions and 0 deletions

View File

@@ -20,6 +20,7 @@
package caps
import (
"encoding/json"
"fmt"
)
@@ -68,3 +69,8 @@ func (t *Type) Validate(c *Capability) error {
// TypeLookupFn aids in looking up a Type by name
type TypeLookupFn func(name string) *Type
// MarshalJSON encodes a Type object as the name of the type.
func (t *Type) MarshalJSON() ([]byte, error) {
return json.Marshal(t.Name)
}

View File

@@ -20,6 +20,7 @@
package caps
import (
"encoding/json"
"testing"
. "gopkg.in/check.v1"
@@ -62,3 +63,9 @@ func (s *TypeSuite) TestValidateAttributes(c *C) {
err := testType.Validate(cap)
c.Assert(err, ErrorMatches, "attributes must be empty for now")
}
func (s *TypeSuite) TestMarhshalJSON(c *C) {
b, err := json.Marshal(testType)
c.Assert(err, IsNil)
c.Assert(b, DeepEquals, []byte(`"test"`))
}