69 lines
1.9 KiB
Go
69 lines
1.9 KiB
Go
package goap
|
|
|
|
// Interface for every struct that can be part of an ActivityPub object
|
|
type BaseApChain interface {
|
|
// Get the next "object" in the chain (or self if last element in chain)
|
|
// Though preferably the last element should always be a BaseObject
|
|
// The 2nd parameter indicates whether the returned value is different from the one the function is called on
|
|
// So true => Is a different object, false => is self
|
|
GetSelfOrBase() (BaseApChain, bool)
|
|
// Convert the chain to a map
|
|
// Should include the rest of the chain (extend the map the underlying object returns)
|
|
MarshalToMap() map[string]any
|
|
}
|
|
|
|
// Func used to add parsers for other attributes not yet included in the library
|
|
// It is expected that, on success, the function removes its key from the raw map
|
|
type UnmarshalFunc func(map[string]any, BaseApChain) (BaseApChain, error)
|
|
|
|
// The minimum data every AP object has
|
|
type BaseObject struct {
|
|
Id string
|
|
Type string
|
|
}
|
|
|
|
func (b *BaseObject) GetSelfOrBase() (BaseApChain, bool) {
|
|
return b, false
|
|
}
|
|
|
|
func (b *BaseObject) MarshalToMap() map[string]any {
|
|
return map[string]any{
|
|
KEY_ID: b.Id,
|
|
KEY_TYPE: b.Type,
|
|
}
|
|
}
|
|
|
|
func UnmarshalBaseObject(raw map[string]any, _ BaseApChain) (BaseApChain, error) {
|
|
rawId, ok := raw[KEY_ID]
|
|
if !ok {
|
|
return nil, NoRequiredFieldError{KEY_ID}
|
|
}
|
|
id, ok := rawId.(string)
|
|
if !ok {
|
|
return nil, BadFieldValueError[string]{KEY_ID, rawId, ""}
|
|
}
|
|
|
|
rawObjType, ok := raw[KEY_TYPE]
|
|
if !ok {
|
|
return nil, NoRequiredFieldError{KEY_TYPE}
|
|
}
|
|
objType, ok := rawObjType.([]string)
|
|
if !ok {
|
|
return nil, BadFieldValueError[[]string]{KEY_TYPE, rawObjType, []string{}}
|
|
}
|
|
|
|
return &BaseObject{
|
|
Id: id,
|
|
Type: objType[0],
|
|
}, nil
|
|
}
|
|
|
|
type EmptyBaseObject struct{}
|
|
|
|
func (e *EmptyBaseObject) GetSelfOrBase() (BaseApChain, bool) {
|
|
return e, false
|
|
}
|
|
|
|
func (e *EmptyBaseObject) MarshalToMap() map[string]any {
|
|
return map[string]any{}
|
|
}
|