edge/pkg/storage/driver/rpc/server/share/serializable.go
William Petit 8e574c299b
All checks were successful
arcad/edge/pipeline/pr-master This commit looks good
feat(storage): rpc based implementation
2023-09-28 12:36:30 -06:00

95 lines
2.1 KiB
Go

package share
import (
"time"
"forge.cadoles.com/arcad/edge/pkg/app"
"forge.cadoles.com/arcad/edge/pkg/storage/share"
)
func FromResource(res share.Resource) *SerializableResource {
serializableAttributes := make([]*SerializableAttribute, len(res.Attributes()))
for attrIdx, attr := range res.Attributes() {
serializableAttributes[attrIdx] = FromAttribute(attr)
}
return &SerializableResource{
ID_: res.ID(),
Origin_: res.Origin(),
Attributes_: serializableAttributes,
}
}
func FromAttribute(attr share.Attribute) *SerializableAttribute {
return &SerializableAttribute{
Name_: attr.Name(),
Value_: attr.Value(),
Type_: attr.Type(),
UpdatedAt_: attr.UpdatedAt(),
CreatedAt_: attr.CreatedAt(),
}
}
type SerializableResource struct {
ID_ share.ResourceID
Origin_ app.ID
Attributes_ []*SerializableAttribute
}
// Attributes implements share.Resource.
func (r *SerializableResource) Attributes() []share.Attribute {
attributes := make([]share.Attribute, len(r.Attributes_))
for idx, attr := range r.Attributes_ {
attributes[idx] = attr
}
return attributes
}
// ID implements share.Resource.
func (r *SerializableResource) ID() share.ResourceID {
return r.ID_
}
// Origin implements share.Resource.
func (r *SerializableResource) Origin() app.ID {
return r.Origin_
}
var _ share.Resource = &SerializableResource{}
type SerializableAttribute struct {
Name_ string
Value_ any
Type_ share.ValueType
UpdatedAt_ time.Time
CreatedAt_ time.Time
}
// CreatedAt implements share.Attribute.
func (a *SerializableAttribute) CreatedAt() time.Time {
return a.CreatedAt_
}
// Name implements share.Attribute.
func (a *SerializableAttribute) Name() string {
return a.Name_
}
// Type implements share.Attribute.
func (a *SerializableAttribute) Type() share.ValueType {
return a.Type_
}
// UpdatedAt implements share.Attribute.
func (a *SerializableAttribute) UpdatedAt() time.Time {
return a.UpdatedAt_
}
// Value implements share.Attribute.
func (a *SerializableAttribute) Value() any {
return a.Value_
}
var _ share.Attribute = &SerializableAttribute{}