129 lines
2.6 KiB
Go
129 lines
2.6 KiB
Go
// Package models provides the models for the KermaGo application
|
|
package models
|
|
|
|
import (
|
|
"errors"
|
|
|
|
"github.com/docker/go/canonical/json"
|
|
)
|
|
|
|
/*
|
|
An Object is a struct that represents a Kerma object. The object has a type and the object specification as a raw JSON
|
|
*/
|
|
type Object struct {
|
|
Type string `json:"type" binding:"required"`
|
|
Object json.RawMessage `json:"object" binding:"required"`
|
|
}
|
|
|
|
/*
|
|
An ObjectWrapper is a struct that wraps a Kerma object. The wrapper has a type and an object ID
|
|
*/
|
|
type ObjectWrapper struct {
|
|
Type string `json:"type" binding:"required"`
|
|
ObjectID string `json:"objectid" binding:"required"`
|
|
}
|
|
|
|
/*
|
|
BuildObjectRequest creates a new ObjectWrapper with type "getobject" and the supplied Object ID
|
|
*/
|
|
func (o *ObjectWrapper) BuildObjectRequest(oid string) {
|
|
o.Type = "getobject"
|
|
o.ObjectID = oid
|
|
}
|
|
|
|
/*
|
|
BuildGossipObject creates a new ObjectWrapper with type "ihaveobject" and the supplied Object ID
|
|
*/
|
|
func (o *ObjectWrapper) BuildGossipObject(oid string) {
|
|
o.Type = "ihaveobject"
|
|
o.ObjectID = oid
|
|
}
|
|
|
|
/*
|
|
MarshalJson returns the canonical JSON of the object wrapper
|
|
*/
|
|
func (o *ObjectWrapper) MarshalJson() ([]byte, error) {
|
|
return json.MarshalCanonical(o)
|
|
}
|
|
|
|
/*
|
|
UnmarshalJSON creates a new ObjectWrapper from the input JSON byte array
|
|
*/
|
|
func (o *ObjectWrapper) UnmarshalJSON(data []byte) error {
|
|
if len(data) == 0 {
|
|
return nil
|
|
}
|
|
|
|
type tmp ObjectWrapper
|
|
err := json.Unmarshal(data, (*tmp)(o))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if o.Type != "getobject" && o.Type != "ihaveobject" {
|
|
return errors.New("inccorect type for object supplied")
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
/*
|
|
GetObjectValue returns the value of the object as an IEntity
|
|
*/
|
|
func (o *Object) GetObjectValue() (IEntity, error) {
|
|
msg, err := o.Object.MarshalJSON()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var transaction Transaction
|
|
terr := transaction.UnmarshalJSON(msg)
|
|
if terr == nil {
|
|
return &transaction, nil
|
|
}
|
|
|
|
var block Block
|
|
berr := block.UnmarshalJSON(msg)
|
|
if berr == nil {
|
|
return &block, nil
|
|
}
|
|
|
|
return nil, berr
|
|
}
|
|
|
|
/*
|
|
MarshalJson returns the canonical JSON of the object
|
|
*/
|
|
func (o *Object) MarshalJson() ([]byte, error) {
|
|
return json.MarshalCanonical(o)
|
|
}
|
|
|
|
/*
|
|
UnmarshalJSON creates a new object from the input JSON byte array
|
|
*/
|
|
func (o *Object) UnmarshalJSON(data []byte) error {
|
|
if len(data) == 0 {
|
|
return nil
|
|
}
|
|
|
|
type tmp Object
|
|
err := json.Unmarshal(data, (*tmp)(o))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if o.Type != "object" {
|
|
return errors.New("inccorect type for object supplied")
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
/*
|
|
String returns the canonical JSON of the object as a string
|
|
*/
|
|
func (o *Object) String() string {
|
|
res, _ := o.MarshalJson()
|
|
return string(res)
|
|
}
|