diff options
author | 2021-08-12 21:03:24 +0200 | |
---|---|---|
committer | 2021-08-12 21:03:24 +0200 | |
commit | 98263a7de64269898a2f81207e38943b5c8e8653 (patch) | |
tree | 743c90f109a6c5d27832d1dcef2388d939f0f77a /vendor/github.com/go-fed/activity/streams/util.go | |
parent | Text duplication fix (#137) (diff) | |
download | gotosocial-98263a7de64269898a2f81207e38943b5c8e8653.tar.xz |
Grand test fixup (#138)
* start fixing up tests
* fix up tests + automate with drone
* fiddle with linting
* messing about with drone.yml
* some more fiddling
* hmmm
* add cache
* add vendor directory
* verbose
* ci updates
* update some little things
* update sig
Diffstat (limited to 'vendor/github.com/go-fed/activity/streams/util.go')
-rw-r--r-- | vendor/github.com/go-fed/activity/streams/util.go | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/vendor/github.com/go-fed/activity/streams/util.go b/vendor/github.com/go-fed/activity/streams/util.go new file mode 100644 index 000000000..ea27f2f96 --- /dev/null +++ b/vendor/github.com/go-fed/activity/streams/util.go @@ -0,0 +1,66 @@ +package streams + +import ( + "github.com/go-fed/activity/streams/vocab" +) + +const ( + // jsonLDContext is the key for the JSON-LD specification's context + // value. It contains the definitions of the types contained within the + // rest of the payload. Important for linked-data representations, but + // only applicable to go-fed at code-generation time. + jsonLDContext = "@context" +) + +// Serialize adds the context vocabularies contained within the type +// into the JSON-LD @context field, and aliases them appropriately. +func Serialize(a vocab.Type) (m map[string]interface{}, e error) { + m, e = a.Serialize() + if e != nil { + return + } + v := a.JSONLDContext() + // Transform the map of vocabulary-to-aliases into a context payload, + // but do so in a way that at least keeps it readable for other humans. + var contextValue interface{} + if len(v) == 1 { + for vocab, alias := range v { + if len(alias) == 0 { + contextValue = vocab + } else { + contextValue = map[string]string{ + alias: vocab, + } + } + } + } else { + var arr []interface{} + aliases := make(map[string]string) + for vocab, alias := range v { + if len(alias) == 0 { + arr = append(arr, vocab) + } else { + aliases[alias] = vocab + } + } + if len(aliases) > 0 { + arr = append(arr, aliases) + } + contextValue = arr + } + // TODO: Update the context instead if it already exists + m[jsonLDContext] = contextValue + // TODO: Sort the context based on arbitrary order. + // Delete any existing `@context` in child maps. + var cleanFnRecur func(map[string]interface{}) + cleanFnRecur = func(r map[string]interface{}) { + for _, v := range r { + if n, ok := v.(map[string]interface{}); ok { + delete(n, jsonLDContext) + cleanFnRecur(n) + } + } + } + cleanFnRecur(m) + return +} |