Skip to content

Commit

Permalink
improvements to Collection, Endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
MatejLach committed Sep 29, 2024
1 parent 0011686 commit e8b6823
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 57 deletions.
19 changes: 7 additions & 12 deletions asvocab_base_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,6 @@ type Icons struct {
URL string
}

type EndpointsOrString struct {
Endpoints Endpoint
URL string
}

// https://www.w3.org/TR/activitystreams-vocabulary

// Object represents the base ActivityStreams Object and all of its properties
Expand Down Expand Up @@ -128,7 +123,7 @@ type Location struct {
Units string `json:"units,omitempty"`
}

type Endpoint struct {
type Endpoints struct {
OauthAuthorizationEndpoint string `json:"oauthAuthorizationEndpoint,omitempty"`
OauthTokenEndpoint string `json:"oauthTokenEndpoint,omitempty"`
ProvideClientKey string `json:"provideClientKey,omitempty"`
Expand All @@ -149,7 +144,7 @@ type Actor struct {

Devices *StringWithCollection `json:"devices,omitempty"`
Discoverable *bool `json:"discoverable,omitempty"`
EndpointsOrURI *EndpointsOrString `json:"endpoints,omitempty"`
Endpoints *Endpoints `json:"endpoints,omitempty"`
Featured *StringWithOrderedCollection `json:"featured,omitempty"`
FeaturedTags *StringWithCollection `json:"featuredTags,omitempty"`
Followers *StringWithOrderedCollection `json:"followers,omitempty"`
Expand Down Expand Up @@ -203,11 +198,11 @@ type CollectionPage struct {
type Collection struct {
Object

Current *ObjectOrLinkOrString `json:"current,omitempty"`
First *ObjectOrLinkOrString `json:"first,omitempty"`
Items *ObjectOrLinkOrString `json:"items,omitempty"`
Last *ObjectOrLinkOrString `json:"last,omitempty"`
TotalItems int `json:"totalItems,omitempty"`
Current *StringWithCollectionPage `json:"current,omitempty"`
First *StringWithCollectionPage `json:"first,omitempty"`
Items *ObjectOrLinkOrString `json:"items,omitempty"`
Last *StringWithCollectionPage `json:"last,omitempty"`
TotalItems int `json:"totalItems,omitempty"`
}

// OrderedCollectionPage implements https://golang.org/pkg/sort/#Interface
Expand Down
49 changes: 4 additions & 45 deletions asvocab_serialization.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,24 +217,6 @@ func (sc *StringWithCollection) UnmarshalJSON(data []byte) error {
return nil
}

// Implements https://golang.org/pkg/encoding/json/#Unmarshaler
func (enp *EndpointsOrString) UnmarshalJSON(data []byte) error {
if bytes.HasPrefix(bytes.TrimSpace(data), []byte{'"'}) {
if err := json.Unmarshal(data, &enp.URL); err != nil {
return err
}
if _, err := url.ParseRequestURI(enp.URL); err != nil {
return err
}
} else if bytes.HasPrefix(bytes.TrimSpace(data), []byte{'{'}) {
decoder := json.NewDecoder(bytes.NewReader(data))
if err := decoder.Decode(&enp.Endpoints); err != nil {
return err
}
}
return nil
}

// Implements https://golang.org/pkg/encoding/json/#Marshal
func (ol *ObjectOrLink) MarshalJSON() ([]byte, error) {
if len(*ol) > 0 {
Expand Down Expand Up @@ -384,10 +366,10 @@ func (col *Collection) MarshalJSON() ([]byte, error) {
encodedBase = bytes.TrimSuffix(encodedBase, []byte("}"))

encodedCollection, err := json.Marshal(struct {
TotalItems int `json:"totalItems,omitempty"`
Current *ObjectOrLinkOrString `json:"current,omitempty"`
First *ObjectOrLinkOrString `json:"first,omitempty"`
Last *ObjectOrLinkOrString `json:"last,omitempty"`
TotalItems int `json:"totalItems,omitempty"`
Current *StringWithCollectionPage `json:"current,omitempty"`
First *StringWithCollectionPage `json:"first,omitempty"`
Last *StringWithCollectionPage `json:"last,omitempty"`
}{
TotalItems: col.TotalItems,
Current: col.Current,
Expand Down Expand Up @@ -614,24 +596,6 @@ func (sc *StringWithCollection) MarshalJSON() ([]byte, error) {
return []byte{}, errors.New("unrecognised content, cannot Marshal StringWithCollection, use nil for empty value")
}

// Implements https://golang.org/pkg/encoding/json/#Marshal
func (enp *EndpointsOrString) MarshalJSON() ([]byte, error) {
if len(enp.URL) > 0 {
jsonb, err := json.Marshal(enp.URL)
if err != nil {
return []byte{}, err
}
return jsonb, nil
} else if !((Endpoint{}) == enp.Endpoints) {
jsonb, err := json.Marshal(enp.Endpoints)
if err != nil {
return []byte{}, err
}
return jsonb, nil
}
return []byte{}, errors.New("unrecognised content, cannot Marshal EndpointsOrString, use nil for empty value")
}

// DecodeJSON is the generic unmarshaller for any valid ActivityStreams 2.0 object
func DecodeJSON[T ActivityStreamer](jsonPayload io.Reader) (T, error) {
var result T
Expand Down Expand Up @@ -678,8 +642,3 @@ func (soc *StringWithOrderedCollection) String() string {
func (sc *StringWithCollection) String() string {
return fmt.Sprintf("%+v", *sc)
}

// Implements https://golang.org/pkg/fmt/#Stringer
func (enp *EndpointsOrString) String() string {
return fmt.Sprintf("%+v", *enp)
}

0 comments on commit e8b6823

Please sign in to comment.