Vocabulary
The vocabulary module offers developers a consistent data model that is identical to the types defined in the Activity Vocabulary specification. So the types that developers have to use are easily recognizable and reasoned about if they are already familiar with the specification itself.
Extensions
In addition, the Actor types are extended with the publicKey
property which is required
by the Federation Protocol HTTP-Signatures authorization mechanism.
The types in this module require the JSON-LD functionality for the
marshalling and unmarshalling to be compatible with other ActivityPub services,
the standard library’s json module does not deal with the dynamic nature of the
linked data properties in a correct fashion, see design decisions for
details.
The Item interface
Due to how dynamic the correspondence between JSON-LD properties, and their
values is – which we already mentioned in the previous paragraph – most of our
object properties are typed to the vocab.Item interface1).
Helpers
Another side effect of the fact that most of the library deals with instances of this interface, we needed a mechanism that would help developers writing code for the specific types that implement it.
This was achieved by providing a number of helper functions starting with
OnXXX and ToXXX which give the possibility of treating any struct that
impelments Item as an XXX ActivityPub object, where XXX can be an
Activity, an Actor, Object, Link etc.
The first caveat about this type of logic is that it relies heavily (at least
currently) on the fact that the memory layout for each type needs to be
identical for the properties which are common. The properties need to have the
same order and the same type. Basically the existing ToXXX functions
work as a cast does in plain C. It takes the pointer the interface holds, and
converts it to the desired type (using the unsafe package) relying on the fact
that the common properties of the Objects are at the same offsets from the
pointer3).
This makes it neccessary to unwrap these interfaces into concrete types that a
user might need to use for their application logic. So the module offers a
number of helper functions that can execute closures on the interface values
when they encapsulate a value of the expected type.
The official code
documenatation
has a number of examples for the OnXXX and ToXXX functions which are used
for this purpose.
1) The interface matches the ActivityStreams
separation between Object compatible types and Link compatible types.
2) This also requires overriding the default typer function, which is used to
return the correct type based on the YYY.Type property.
3) This behaviour is risky and it can probably change without warning if the Go dev team changes the language’s memory model at a later date. I hope I’ll find a cleaner way to implemnt this in the future, but for now it serves its purpose.