• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..16-Nov-2018-

README.mdH A D16-Nov-20184.1 KiB139114

gen_accept.goH A D16-Nov-201868.1 KiB2,4571,685

gen_activity.goH A D16-Nov-201868.7 KiB2,4531,682

gen_add.goH A D16-Nov-201867.4 KiB2,4531,682

gen_announce.goH A D16-Nov-201868.6 KiB2,4571,685

gen_application.goH A D16-Nov-201858.9 KiB2,0941,440

gen_arrive.goH A D16-Nov-201866.6 KiB2,4011,648

gen_article.goH A D16-Nov-201858.1 KiB2,0941,440

gen_audio.goH A D16-Nov-201857.7 KiB2,0941,440

gen_block.goH A D16-Nov-201867.9 KiB2,4531,682

gen_collection.goH A D16-Nov-201864.5 KiB2,3071,596

gen_collectionpage.goH A D16-Nov-201868.4 KiB2,4211,683

gen_create.goH A D16-Nov-201867.9 KiB2,4531,682

gen_delete.goH A D16-Nov-201868 KiB2,4531,682

gen_dislike.goH A D16-Nov-201868.2 KiB2,4531,682

gen_document.goH A D16-Nov-201858.3 KiB2,0941,440

gen_event.goH A D16-Nov-201857.7 KiB2,0941,440

gen_flag.goH A D16-Nov-201867.5 KiB2,4531,682

gen_follow.goH A D16-Nov-201868.1 KiB2,4531,682

gen_group.goH A D16-Nov-201857.7 KiB2,0941,440

gen_ignore.goH A D16-Nov-201868 KiB2,4531,682

gen_image.goH A D16-Nov-201859.3 KiB2,1621,490

gen_intransitiveactivity.goH A D16-Nov-201870 KiB2,4011,648

gen_invite.goH A D16-Nov-201868 KiB2,4531,682

gen_join.goH A D16-Nov-201867.5 KiB2,4531,682

gen_leave.goH A D16-Nov-201867.7 KiB2,4531,682

gen_like.goH A D16-Nov-201867.5 KiB2,4531,682

gen_link.goH A D16-Nov-201815.9 KiB582390

gen_listen.goH A D16-Nov-201867.9 KiB2,4531,682

gen_mention.goH A D16-Nov-201815.6 KiB582390

gen_move.goH A D16-Nov-201867.5 KiB2,4531,682

gen_note.goH A D16-Nov-201857.5 KiB2,0941,440

gen_object.goH A D16-Nov-201858.1 KiB2,0941,440

gen_offer.goH A D16-Nov-201867.7 KiB2,4531,682

gen_orderedcollection.goH A D16-Nov-201866.3 KiB2,3071,596

gen_orderedcollectionpage.goH A D16-Nov-201871.3 KiB2,4551,708

gen_organization.goH A D16-Nov-201859.2 KiB2,0941,440

gen_page.goH A D16-Nov-201857.4 KiB2,0941,440

gen_person.goH A D16-Nov-201857.9 KiB2,0941,440

gen_place.goH A D16-Nov-201862 KiB2,2641,565

gen_profile.goH A D16-Nov-201859.2 KiB2,1281,465

gen_question.goH A D16-Nov-201872.6 KiB2,5951,782

gen_read.goH A D16-Nov-201867.4 KiB2,4531,682

gen_reject.goH A D16-Nov-201868 KiB2,4531,682

gen_relationship.goH A D16-Nov-201863.1 KiB2,2271,534

gen_remove.goH A D16-Nov-201868 KiB2,4531,682

gen_service.goH A D16-Nov-201858.1 KiB2,0941,440

gen_streams.goH A D16-Nov-201882 KiB3,0502,762

gen_tentativeaccept.goH A D16-Nov-201870.2 KiB2,4531,682

gen_tentativereject.goH A D16-Nov-201870.2 KiB2,4531,682

gen_tombstone.goH A D16-Nov-201861.2 KiB2,1841,503

gen_travel.goH A D16-Nov-201866.6 KiB2,4011,648

gen_undo.goH A D16-Nov-201867.7 KiB2,4531,682

gen_update.goH A D16-Nov-201868.1 KiB2,4531,682

gen_video.goH A D16-Nov-201857.7 KiB2,0941,440

gen_view.goH A D16-Nov-201867.4 KiB2,4531,682

streams_data_test.goH A D16-Nov-201827.4 KiB1,1321,130

streams_test.goH A D16-Nov-20189 KiB341323

README.md

1# streams
2
3Please read the `README.md` in the `go-fed/activity/vocab` package first. This
4library is a convenience layer on top of the `go-fed/activity/vocab` library, so
5this README builds off of that one.
6
7This library is entirely code-generated by the
8`go-fed/activity/tools/streams/gen` library and `go-fed/activity/tools/streams`
9tool. Run `go generate` to refresh the library, which requires `$GOPATH/bin` to
10be on your `$PATH`.
11
12## What it does
13
14This library provides a `Resolver`, which is simply a collection of callbacks
15that clients can specify to handle specific ActivtyStream data types. The
16`Resolver.Deserialize` method turns a JSON-decoded `map[string]interface{}`
17into its proper type, passed to the corresponding callback.
18
19For example, given the data:
20
21```golang
22{
23  "@context": "https://www.w3.org/ns/activitystreams",
24  "type": "Note",
25  "name": "Equivalent Exchange",
26  "content": "I'll give half of my life to you and you give half of yours to me!",
27  "attachment": "https://example.com/attachment"
28}
29```
30
31in `b []byte` one can do the following:
32
33```golang
34var m map[string]interface{}
35if err := json.Unmarshal(b, &m); err != nil {
36	return err
37}
38r := &Resolver {
39	NoteCallback: func(n *Note) error {
40		// 1) Use the Note concrete type here
41		// 2) Errors are propagated transparently
42	},
43}
44if handled, err := r.Deserialize(m); err != nil {
45	// 3) Any errors from #2 can be handled, or the payload is an unknown type.
46	return err
47} else if !handled {
48	// 4) The callback to handle the concrete type was not set.
49}
50```
51
52Only set the callbacks that are interesting. There is no need to set every
53callback, unless your application requires it.
54
55## Using concrete types
56
57The convenience layer provides easy access to properties with specific types.
58However, because ActivityStreams is fundamentally built off of JSON-LD and
59still permits large degree of freedom when it comes to obtaining a concrete type
60for a property, the convenience API is built to give clients the freedom to
61choose how best to federate.
62
63For every type in this package (except `Resolver`), there is an equivalent type
64in the `activity/vocab` package. It takes only a call to `Raw` to go from this
65convenience API to the full API:
66
67```golang
68r := &Resolver {
69	NoteCallback: func(n *Note) error {
70		// Raw is available for all ActivityStream types
71		vocabNote := n.Raw()
72	},
73}
74```
75
76To determine whether the call to `Raw` is needed, the "get" and "has" methods
77use `Resolution` and `Presence` types to inform client code. The client is free
78to support as many types as is feasible within the specific application.
79
80Reusing the `Note` example above that has an `attachment`, the following is
81client code that tries to handle every possible type that `attachment` can
82take. **The W3C does not require client applications to support all of these
83use cases.**
84
85```golang
86r := &Resolver {}
87r.NoteCallback = func(n *Note) error {
88	if n.LenAttachment() == 1 {
89		if presence := n.HasAttachment(0); p == ConvenientPresence {
90			// A new or existing Resolver can be used. This is the convenient getter.
91			if resolution, err := n.ResolveAttachment(r, 0); err != nil {
92				return err
93			} else if resolution == RawResolutionNeeded {
94				vocabNote := n.Raw()
95				// Use the full API
96				if vocabNote.IsAttachmentIRI(0) {
97					...
98				} else ...
99			}
100		} else if p == RawPresence {
101			vocabNote := n.Raw()
102			// Use the full API
103			if vocabNote.IsAttachmentIRI(0) {
104				...
105			} else ...
106		}
107	}
108}
109```
110
111## Serializing data
112
113Creating a raw type and serializing it is straightforward:
114
115```golang
116n := &Note{}
117n.AddName("I'll see you again")
118n.AddContent("You don't have to be alone when I leave")
119// The "type" property is automatically handled...
120m, err := n.Serialize()
121if err != nil {
122	return err
123}
124// ...but "@context" is not.
125m["@context"] = "https://www.w3.org/ns/activitystreams"
126b, err := json.Marshal(m)
127```
128
129The only caveat is that clients must set `"@context"` manually at this time.
130
131## What it doesn't do
132
133Please see the same section in the `go-fed/activity/vocab` package.
134
135## Other considerations
136
137This library is entirely code-generated. Please see the same section in the
138`go-fed/activity/vocab` package for more details.
139