package propertystreams import ( "fmt" vocab "github.com/go-fed/activity/streams/vocab" "net/url" ) // StreamsPropertyIterator is an iterator for a property. It is permitted to be // one of multiple value types. At most, one type of value can be present, or // none at all. Setting a value will clear the other types of values so that // only one of the 'Is' methods will return true. It is possible to clear all // values, so that this property is empty. type StreamsPropertyIterator struct { OrderedCollectionMember vocab.OrderedCollectionInterface CollectionMember vocab.CollectionInterface CollectionPageMember vocab.CollectionPageInterface OrderedCollectionPageMember vocab.OrderedCollectionPageInterface unknown interface{} iri *url.URL alias string myIdx int parent vocab.StreamsPropertyInterface } // NewStreamsPropertyIterator creates a new streams property. func NewStreamsPropertyIterator() *StreamsPropertyIterator { return &StreamsPropertyIterator{alias: ""} } // deserializeStreamsPropertyIterator creates an iterator from an element that has // been unmarshalled from a text or binary format. func deserializeStreamsPropertyIterator(i interface{}, aliasMap map[string]string) (*StreamsPropertyIterator, error) { alias := "" if a, ok := aliasMap["https://www.w3.org/TR/activitystreams-vocabulary"]; ok { alias = a } if s, ok := i.(string); ok { u, err := url.Parse(s) // If error exists, don't error out -- skip this and treat as unknown string ([]byte) at worst // Also, if no scheme exists, don't treat it as a URL -- net/url is greedy if err == nil && len(u.Scheme) > 0 { this := &StreamsPropertyIterator{ alias: alias, iri: u, } return this, nil } } if m, ok := i.(map[string]interface{}); ok { if v, err := mgr.DeserializeOrderedCollectionActivityStreams()(m, aliasMap); err == nil { this := &StreamsPropertyIterator{ OrderedCollectionMember: v, alias: alias, } return this, nil } else if v, err := mgr.DeserializeCollectionActivityStreams()(m, aliasMap); err == nil { this := &StreamsPropertyIterator{ CollectionMember: v, alias: alias, } return this, nil } else if v, err := mgr.DeserializeCollectionPageActivityStreams()(m, aliasMap); err == nil { this := &StreamsPropertyIterator{ CollectionPageMember: v, alias: alias, } return this, nil } else if v, err := mgr.DeserializeOrderedCollectionPageActivityStreams()(m, aliasMap); err == nil { this := &StreamsPropertyIterator{ OrderedCollectionPageMember: v, alias: alias, } return this, nil } } this := &StreamsPropertyIterator{ alias: alias, unknown: i, } return this, nil } // GetCollection returns the value of this property. When IsCollection returns // false, GetCollection will return an arbitrary value. func (this StreamsPropertyIterator) GetCollection() vocab.CollectionInterface { return this.CollectionMember } // GetCollectionPage returns the value of this property. When IsCollectionPage // returns false, GetCollectionPage will return an arbitrary value. func (this StreamsPropertyIterator) GetCollectionPage() vocab.CollectionPageInterface { return this.CollectionPageMember } // GetIRI returns the IRI of this property. When IsIRI returns false, GetIRI will // return an arbitrary value. func (this StreamsPropertyIterator) GetIRI() *url.URL { return this.iri } // GetOrderedCollection returns the value of this property. When // IsOrderedCollection returns false, GetOrderedCollection will return an // arbitrary value. func (this StreamsPropertyIterator) GetOrderedCollection() vocab.OrderedCollectionInterface { return this.OrderedCollectionMember } // GetOrderedCollectionPage returns the value of this property. When // IsOrderedCollectionPage returns false, GetOrderedCollectionPage will return // an arbitrary value. func (this StreamsPropertyIterator) GetOrderedCollectionPage() vocab.OrderedCollectionPageInterface { return this.OrderedCollectionPageMember } // HasAny returns true if any of the different values is set. func (this StreamsPropertyIterator) HasAny() bool { return this.IsOrderedCollection() || this.IsCollection() || this.IsCollectionPage() || this.IsOrderedCollectionPage() || this.iri != nil } // IsCollection returns true if this property has a type of "Collection". When // true, use the GetCollection and SetCollection methods to access and set // this property. func (this StreamsPropertyIterator) IsCollection() bool { return this.CollectionMember != nil } // IsCollectionPage returns true if this property has a type of "CollectionPage". // When true, use the GetCollectionPage and SetCollectionPage methods to // access and set this property. func (this StreamsPropertyIterator) IsCollectionPage() bool { return this.CollectionPageMember != nil } // IsIRI returns true if this property is an IRI. When true, use GetIRI and SetIRI // to access and set this property func (this StreamsPropertyIterator) IsIRI() bool { return this.iri != nil } // IsOrderedCollection returns true if this property has a type of // "OrderedCollection". When true, use the GetOrderedCollection and // SetOrderedCollection methods to access and set this property. func (this StreamsPropertyIterator) IsOrderedCollection() bool { return this.OrderedCollectionMember != nil } // IsOrderedCollectionPage returns true if this property has a type of // "OrderedCollectionPage". When true, use the GetOrderedCollectionPage and // SetOrderedCollectionPage methods to access and set this property. func (this StreamsPropertyIterator) IsOrderedCollectionPage() bool { return this.OrderedCollectionPageMember != nil } // JSONLDContext returns the JSONLD URIs required in the context string for this // property and the specific values that are set. The value in the map is the // alias used to import the property's value or values. func (this StreamsPropertyIterator) JSONLDContext() map[string]string { m := map[string]string{"https://www.w3.org/TR/activitystreams-vocabulary": this.alias} var child map[string]string if this.IsOrderedCollection() { child = this.GetOrderedCollection().JSONLDContext() } else if this.IsCollection() { child = this.GetCollection().JSONLDContext() } else if this.IsCollectionPage() { child = this.GetCollectionPage().JSONLDContext() } else if this.IsOrderedCollectionPage() { child = this.GetOrderedCollectionPage().JSONLDContext() } /* Since the literal maps in this function are determined at code-generation time, this loop should not overwrite an existing key with a new value. */ for k, v := range child { m[k] = v } return m } // KindIndex computes an arbitrary value for indexing this kind of value. This is // a leaky API detail only for folks looking to replace the go-fed // implementation. Applications should not use this method. func (this StreamsPropertyIterator) KindIndex() int { if this.IsOrderedCollection() { return 0 } if this.IsCollection() { return 1 } if this.IsCollectionPage() { return 2 } if this.IsOrderedCollectionPage() { return 3 } if this.IsIRI() { return -2 } return -1 } // LessThan compares two instances of this property with an arbitrary but stable // comparison. Applications should not use this because it is only meant to // help alternative implementations to go-fed to be able to normalize // nonfunctional properties. func (this StreamsPropertyIterator) LessThan(o vocab.StreamsPropertyIteratorInterface) bool { idx1 := this.KindIndex() idx2 := o.KindIndex() if idx1 < idx2 { return true } else if idx1 > idx2 { return false } else if this.IsOrderedCollection() { return this.GetOrderedCollection().LessThan(o.GetOrderedCollection()) } else if this.IsCollection() { return this.GetCollection().LessThan(o.GetCollection()) } else if this.IsCollectionPage() { return this.GetCollectionPage().LessThan(o.GetCollectionPage()) } else if this.IsOrderedCollectionPage() { return this.GetOrderedCollectionPage().LessThan(o.GetOrderedCollectionPage()) } else if this.IsIRI() { return this.iri.String() < o.GetIRI().String() } return false } // Name returns the name of this property: "streams". func (this StreamsPropertyIterator) Name() string { return "streams" } // Next returns the next iterator, or nil if there is no next iterator. func (this StreamsPropertyIterator) Next() vocab.StreamsPropertyIteratorInterface { if this.myIdx+1 >= this.parent.Len() { return nil } else { return this.parent.At(this.myIdx + 1) } } // Prev returns the previous iterator, or nil if there is no previous iterator. func (this StreamsPropertyIterator) Prev() vocab.StreamsPropertyIteratorInterface { if this.myIdx-1 < 0 { return nil } else { return this.parent.At(this.myIdx - 1) } } // SetCollection sets the value of this property. Calling IsCollection afterwards // returns true. func (this *StreamsPropertyIterator) SetCollection(v vocab.CollectionInterface) { this.clear() this.CollectionMember = v } // SetCollectionPage sets the value of this property. Calling IsCollectionPage // afterwards returns true. func (this *StreamsPropertyIterator) SetCollectionPage(v vocab.CollectionPageInterface) { this.clear() this.CollectionPageMember = v } // SetIRI sets the value of this property. Calling IsIRI afterwards returns true. func (this *StreamsPropertyIterator) SetIRI(v *url.URL) { this.clear() this.iri = v } // SetOrderedCollection sets the value of this property. Calling // IsOrderedCollection afterwards returns true. func (this *StreamsPropertyIterator) SetOrderedCollection(v vocab.OrderedCollectionInterface) { this.clear() this.OrderedCollectionMember = v } // SetOrderedCollectionPage sets the value of this property. Calling // IsOrderedCollectionPage afterwards returns true. func (this *StreamsPropertyIterator) SetOrderedCollectionPage(v vocab.OrderedCollectionPageInterface) { this.clear() this.OrderedCollectionPageMember = v } // clear ensures no value of this property is set. Calling HasAny or any of the // 'Is' methods afterwards will return false. func (this *StreamsPropertyIterator) clear() { this.OrderedCollectionMember = nil this.CollectionMember = nil this.CollectionPageMember = nil this.OrderedCollectionPageMember = nil this.unknown = nil this.iri = nil } // serialize converts this into an interface representation suitable for // marshalling into a text or binary format. Applications should not need this // function as most typical use cases serialize types instead of individual // properties. It is exposed for alternatives to go-fed implementations to use. func (this StreamsPropertyIterator) serialize() (interface{}, error) { if this.IsOrderedCollection() { return this.GetOrderedCollection().Serialize() } else if this.IsCollection() { return this.GetCollection().Serialize() } else if this.IsCollectionPage() { return this.GetCollectionPage().Serialize() } else if this.IsOrderedCollectionPage() { return this.GetOrderedCollectionPage().Serialize() } else if this.IsIRI() { return this.iri.String(), nil } return this.unknown, nil } // StreamsProperty is the non-functional property "streams". It is permitted to // have one or more values, and of different value types. type StreamsProperty struct { properties []*StreamsPropertyIterator alias string } // DeserializeStreamsProperty creates a "streams" property from an interface // representation that has been unmarshalled from a text or binary format. func DeserializeStreamsProperty(m map[string]interface{}, aliasMap map[string]string) (vocab.StreamsPropertyInterface, error) { alias := "" if a, ok := aliasMap["https://www.w3.org/TR/activitystreams-vocabulary"]; ok { alias = a } propName := "streams" if len(alias) > 0 { propName = fmt.Sprintf("%s:%s", alias, "streams") } i, ok := m[propName] if ok { this := &StreamsProperty{ alias: alias, properties: []*StreamsPropertyIterator{}, } if list, ok := i.([]interface{}); ok { for _, iterator := range list { if p, err := deserializeStreamsPropertyIterator(iterator, aliasMap); err != nil { return this, err } else if p != nil { this.properties = append(this.properties, p) } } } else { if p, err := deserializeStreamsPropertyIterator(i, aliasMap); err != nil { return this, err } else if p != nil { this.properties = append(this.properties, p) } } // Set up the properties for iteration. for idx, ele := range this.properties { ele.parent = this ele.myIdx = idx } return this, nil } return nil, nil } // NewStreamsProperty creates a new streams property. func NewStreamsProperty() *StreamsProperty { return &StreamsProperty{alias: ""} } // AppendCollection appends a Collection value to the back of a list of the // property "streams". Invalidates iterators that are traversing using Prev. func (this *StreamsProperty) AppendCollection(v vocab.CollectionInterface) { this.properties = append(this.properties, &StreamsPropertyIterator{ CollectionMember: v, alias: this.alias, myIdx: this.Len(), parent: this, }) } // AppendCollectionPage appends a CollectionPage value to the back of a list of // the property "streams". Invalidates iterators that are traversing using // Prev. func (this *StreamsProperty) AppendCollectionPage(v vocab.CollectionPageInterface) { this.properties = append(this.properties, &StreamsPropertyIterator{ CollectionPageMember: v, alias: this.alias, myIdx: this.Len(), parent: this, }) } // AppendIRI appends an IRI value to the back of a list of the property "streams" func (this *StreamsProperty) AppendIRI(v *url.URL) { this.properties = append(this.properties, &StreamsPropertyIterator{ alias: this.alias, iri: v, myIdx: this.Len(), parent: this, }) } // AppendOrderedCollection appends a OrderedCollection value to the back of a list // of the property "streams". Invalidates iterators that are traversing using // Prev. func (this *StreamsProperty) AppendOrderedCollection(v vocab.OrderedCollectionInterface) { this.properties = append(this.properties, &StreamsPropertyIterator{ OrderedCollectionMember: v, alias: this.alias, myIdx: this.Len(), parent: this, }) } // AppendOrderedCollectionPage appends a OrderedCollectionPage value to the back // of a list of the property "streams". Invalidates iterators that are // traversing using Prev. func (this *StreamsProperty) AppendOrderedCollectionPage(v vocab.OrderedCollectionPageInterface) { this.properties = append(this.properties, &StreamsPropertyIterator{ OrderedCollectionPageMember: v, alias: this.alias, myIdx: this.Len(), parent: this, }) } // At returns the property value for the specified index. Panics if the index is // out of bounds. func (this StreamsProperty) At(index int) vocab.StreamsPropertyIteratorInterface { return this.properties[index] } // Begin returns the first iterator, or nil if empty. Can be used with the // iterator's Next method and this property's End method to iterate from front // to back through all values. func (this StreamsProperty) Begin() vocab.StreamsPropertyIteratorInterface { if this.Empty() { return nil } else { return this.properties[0] } } // Empty returns returns true if there are no elements. func (this StreamsProperty) Empty() bool { return this.Len() == 0 } // End returns beyond-the-last iterator, which is nil. Can be used with the // iterator's Next method and this property's Begin method to iterate from // front to back through all values. func (this StreamsProperty) End() vocab.StreamsPropertyIteratorInterface { return nil } // JSONLDContext returns the JSONLD URIs required in the context string for this // property and the specific values that are set. The value in the map is the // alias used to import the property's value or values. func (this StreamsProperty) JSONLDContext() map[string]string { m := map[string]string{"https://www.w3.org/TR/activitystreams-vocabulary": this.alias} for _, elem := range this.properties { child := elem.JSONLDContext() /* Since the literal maps in this function are determined at code-generation time, this loop should not overwrite an existing key with a new value. */ for k, v := range child { m[k] = v } } return m } // KindIndex computes an arbitrary value for indexing this kind of value. This is // a leaky API method specifically needed only for alternate implementations // for go-fed. Applications should not use this method. Panics if the index is // out of bounds. func (this StreamsProperty) KindIndex(idx int) int { return this.properties[idx].KindIndex() } // Len returns the number of values that exist for the "streams" property. func (this StreamsProperty) Len() (length int) { return len(this.properties) } // Less computes whether another property is less than this one. Mixing types // results in a consistent but arbitrary ordering func (this StreamsProperty) Less(i, j int) bool { idx1 := this.KindIndex(i) idx2 := this.KindIndex(j) if idx1 < idx2 { return true } else if idx1 == idx2 { if idx1 == 0 { lhs := this.properties[i].GetOrderedCollection() rhs := this.properties[j].GetOrderedCollection() return lhs.LessThan(rhs) } else if idx1 == 1 { lhs := this.properties[i].GetCollection() rhs := this.properties[j].GetCollection() return lhs.LessThan(rhs) } else if idx1 == 2 { lhs := this.properties[i].GetCollectionPage() rhs := this.properties[j].GetCollectionPage() return lhs.LessThan(rhs) } else if idx1 == 3 { lhs := this.properties[i].GetOrderedCollectionPage() rhs := this.properties[j].GetOrderedCollectionPage() return lhs.LessThan(rhs) } else if idx1 == -2 { lhs := this.properties[i].GetIRI() rhs := this.properties[j].GetIRI() return lhs.String() < rhs.String() } } return false } // LessThan compares two instances of this property with an arbitrary but stable // comparison. Applications should not use this because it is only meant to // help alternative implementations to go-fed to be able to normalize // nonfunctional properties. func (this StreamsProperty) LessThan(o vocab.StreamsPropertyInterface) bool { l1 := this.Len() l2 := o.Len() l := l1 if l2 < l1 { l = l2 } for i := 0; i < l; i++ { if this.properties[i].LessThan(o.At(i)) { return true } else if o.At(i).LessThan(this.properties[i]) { return false } } return l1 < l2 } // Name returns the name of this property: "streams". func (this StreamsProperty) Name() string { return "streams" } // PrependCollection prepends a Collection value to the front of a list of the // property "streams". Invalidates all iterators. func (this *StreamsProperty) PrependCollection(v vocab.CollectionInterface) { this.properties = append([]*StreamsPropertyIterator{{ CollectionMember: v, alias: this.alias, myIdx: 0, parent: this, }}, this.properties...) for i := 1; i < this.Len(); i++ { (this.properties)[i].myIdx = i } } // PrependCollectionPage prepends a CollectionPage value to the front of a list of // the property "streams". Invalidates all iterators. func (this *StreamsProperty) PrependCollectionPage(v vocab.CollectionPageInterface) { this.properties = append([]*StreamsPropertyIterator{{ CollectionPageMember: v, alias: this.alias, myIdx: 0, parent: this, }}, this.properties...) for i := 1; i < this.Len(); i++ { (this.properties)[i].myIdx = i } } // PrependIRI prepends an IRI value to the front of a list of the property // "streams". func (this *StreamsProperty) PrependIRI(v *url.URL) { this.properties = append([]*StreamsPropertyIterator{{ alias: this.alias, iri: v, myIdx: 0, parent: this, }}, this.properties...) for i := 1; i < this.Len(); i++ { (this.properties)[i].myIdx = i } } // PrependOrderedCollection prepends a OrderedCollection value to the front of a // list of the property "streams". Invalidates all iterators. func (this *StreamsProperty) PrependOrderedCollection(v vocab.OrderedCollectionInterface) { this.properties = append([]*StreamsPropertyIterator{{ OrderedCollectionMember: v, alias: this.alias, myIdx: 0, parent: this, }}, this.properties...) for i := 1; i < this.Len(); i++ { (this.properties)[i].myIdx = i } } // PrependOrderedCollectionPage prepends a OrderedCollectionPage value to the // front of a list of the property "streams". Invalidates all iterators. func (this *StreamsProperty) PrependOrderedCollectionPage(v vocab.OrderedCollectionPageInterface) { this.properties = append([]*StreamsPropertyIterator{{ OrderedCollectionPageMember: v, alias: this.alias, myIdx: 0, parent: this, }}, this.properties...) for i := 1; i < this.Len(); i++ { (this.properties)[i].myIdx = i } } // Remove deletes an element at the specified index from a list of the property // "streams", regardless of its type. Panics if the index is out of bounds. // Invalidates all iterators. func (this *StreamsProperty) Remove(idx int) { (this.properties)[idx].parent = nil copy((this.properties)[idx:], (this.properties)[idx+1:]) (this.properties)[len(this.properties)-1] = &StreamsPropertyIterator{} this.properties = (this.properties)[:len(this.properties)-1] for i := idx; i < this.Len(); i++ { (this.properties)[i].myIdx = i } } // Serialize converts this into an interface representation suitable for // marshalling into a text or binary format. Applications should not need this // function as most typical use cases serialize types instead of individual // properties. It is exposed for alternatives to go-fed implementations to use. func (this StreamsProperty) Serialize() (interface{}, error) { s := make([]interface{}, 0, len(this.properties)) for _, iterator := range this.properties { if b, err := iterator.serialize(); err != nil { return s, err } else { s = append(s, b) } } // Shortcut: if serializing one value, don't return an array -- pretty sure other Fediverse software would choke on a "type" value with array, for example. if len(s) == 1 { return s[0], nil } return s, nil } // SetCollection sets a Collection value to be at the specified index for the // property "streams". Panics if the index is out of bounds. Invalidates all // iterators. func (this *StreamsProperty) SetCollection(idx int, v vocab.CollectionInterface) { (this.properties)[idx].parent = nil (this.properties)[idx] = &StreamsPropertyIterator{ CollectionMember: v, alias: this.alias, myIdx: idx, parent: this, } } // SetCollectionPage sets a CollectionPage value to be at the specified index for // the property "streams". Panics if the index is out of bounds. Invalidates // all iterators. func (this *StreamsProperty) SetCollectionPage(idx int, v vocab.CollectionPageInterface) { (this.properties)[idx].parent = nil (this.properties)[idx] = &StreamsPropertyIterator{ CollectionPageMember: v, alias: this.alias, myIdx: idx, parent: this, } } // SetIRI sets an IRI value to be at the specified index for the property // "streams". Panics if the index is out of bounds. func (this *StreamsProperty) SetIRI(idx int, v *url.URL) { (this.properties)[idx].parent = nil (this.properties)[idx] = &StreamsPropertyIterator{ alias: this.alias, iri: v, myIdx: idx, parent: this, } } // SetOrderedCollection sets a OrderedCollection value to be at the specified // index for the property "streams". Panics if the index is out of bounds. // Invalidates all iterators. func (this *StreamsProperty) SetOrderedCollection(idx int, v vocab.OrderedCollectionInterface) { (this.properties)[idx].parent = nil (this.properties)[idx] = &StreamsPropertyIterator{ OrderedCollectionMember: v, alias: this.alias, myIdx: idx, parent: this, } } // SetOrderedCollectionPage sets a OrderedCollectionPage value to be at the // specified index for the property "streams". Panics if the index is out of // bounds. Invalidates all iterators. func (this *StreamsProperty) SetOrderedCollectionPage(idx int, v vocab.OrderedCollectionPageInterface) { (this.properties)[idx].parent = nil (this.properties)[idx] = &StreamsPropertyIterator{ OrderedCollectionPageMember: v, alias: this.alias, myIdx: idx, parent: this, } } // Swap swaps the location of values at two indices for the "streams" property. func (this StreamsProperty) Swap(i, j int) { this.properties[i], this.properties[j] = this.properties[j], this.properties[i] }