-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
143 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |