Skip to content

Commit

Permalink
Merge 2c0ea98 into d4d0837
Browse files Browse the repository at this point in the history
  • Loading branch information
nukosuke committed May 13, 2019
2 parents d4d0837 + 2c0ea98 commit a80fc8f
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 0 deletions.
94 changes: 94 additions & 0 deletions zendesk/collaborators.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package zendesk

import (
"encoding/json"
"fmt"
"reflect"
)

type Collaborator struct {
Name string `json:"name,omitempty"`
Email string `json:"email,omitempty"`
}

type Collaborators struct {
collaborators []interface{}
}

func (c *Collaborators) String() string {
return fmt.Sprintf("%v", c.collaborators)
}

func (c *Collaborators) List() []interface{} {
return c.collaborators
}

func (c *Collaborators) Append(i interface{}) error {
switch e := i.(type) {
case string:
c.collaborators = append(c.collaborators, e)
case Collaborator:
c.collaborators = append(c.collaborators, e)
case int64:
c.collaborators = append(c.collaborators, e)
case map[string]interface{}:
// This might be better suited in UnmarshalJSON
collab := Collaborator{}
name, ok := e["name"]
if !ok {
return fmt.Errorf("map %v did not contain a name value", e)
}

collab.Name, ok = name.(string)
if !ok {
return fmt.Errorf("type of name %v was not string", name)
}

email, ok := e["email"]
if !ok {
return fmt.Errorf("map %v did not contain an email value", e)
}

collab.Email, ok = email.(string)
if !ok {
return fmt.Errorf("type of email %v was not string", name)
}
c.collaborators = append(c.collaborators, collab)
default:
return fmt.Errorf("unsupported collaborator type %v", reflect.TypeOf(i))
}

return nil
}

func (c *Collaborators) MarshalJSON() ([]byte, error) {
return json.Marshal(c.collaborators)
}

func (c *Collaborators) UnmarshalJSON(b []byte) error {
var tmpCollaborators []interface{}
newCollaborators := Collaborators{}
err := json.Unmarshal(b, &tmpCollaborators)
if err != nil {
return err
}

for _, i := range tmpCollaborators {
var err error
switch e := i.(type) {
case float64:
err = newCollaborators.Append(int64(e))
default:
err = newCollaborators.Append(i)
}

if err != nil {
return err
}
}


// possibly validate that there aren't unexpected types in the slice
c.collaborators = newCollaborators.List()
return nil
}
49 changes: 49 additions & 0 deletions zendesk/collaborators_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package zendesk

import (
"reflect"
"testing"
)

const collaboratorListJson = `[562,"someone@example.com",{"name":"SomeoneElse","email":"else@example.com"}]`

func TestCanBeUnmarshalled(t *testing.T) {
c := &Collaborators{}
err := c.UnmarshalJSON([]byte(collaboratorListJson))
if err != nil {
t.Fatalf("Unmarshal returned an error %v", err)
}

list := c.List()
if len(list) != 3 {
t.Fatalf("Collaborators %v did not have the correct length when unmarshaled", c)
}

for _, i := range list {
switch i.(type) {
case string:
case int64:
case Collaborator:
//do nothing
default:
t.Fatalf("List contained an unexpected type %v", reflect.TypeOf(i))
}
}
}

func TestCanBeMarshalled(t *testing.T) {
c := &Collaborators{}
err := c.UnmarshalJSON([]byte(collaboratorListJson))
if err != nil {
t.Fatalf("Unmarshal returned an error %v", err)
}

out, err := c.MarshalJSON()
if err != nil {
t.Fatalf("Marshal returned an error %v", err)
}

if string(out) != collaboratorListJson {
t.Fatalf("Json output %s did not match expected output %s", out, collaboratorListJson)
}
}

0 comments on commit a80fc8f

Please sign in to comment.