Created
September 26, 2017 09:10
-
-
Save hlubek/6e578dcbedcfaec3e65ec593eb74c7bb to your computer and use it in GitHub Desktop.
Value objects in Go with private fields
This file contains hidden or 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
| package valueobjects | |
| import ( | |
| "encoding/json" | |
| "fmt" | |
| ) | |
| type NodeIdentifier struct { | |
| identifier string | |
| } | |
| func NewNodeIdentifier(identifier string) (NodeIdentifier, error) { | |
| var v NodeIdentifier | |
| if err := v.setIdentifier(identifier); err != nil { | |
| return v, err | |
| } | |
| return v, nil | |
| } | |
| func (v *NodeIdentifier) setIdentifier(identifier string) error { | |
| if identifier == "" { | |
| return fmt.Errorf("Identifier must not be empty") | |
| } | |
| v.identifier = identifier | |
| return nil | |
| } | |
| func (v NodeIdentifier) Identifier() string { | |
| return v.identifier | |
| } | |
| func (v NodeIdentifier) MarshalJSON() ([]byte, error) { | |
| return json.Marshal(struct { | |
| Identifier string `json:"identifier"` | |
| }{ | |
| Identifier: v.identifier, | |
| }) | |
| } | |
| func (v *NodeIdentifier) UnmarshalJSON(b []byte) error { | |
| var d struct { | |
| Identifier string `json:"identifier"` | |
| } | |
| if err := json.Unmarshal(b, &d); err != nil { | |
| return err | |
| } | |
| if err := v.setIdentifier(d.Identifier); err != nil { | |
| return err | |
| } | |
| return nil | |
| } |
This file contains hidden or 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
| package valueobjects_test | |
| import ( | |
| "encoding/json" | |
| "testing" | |
| "github.com/stretchr/testify/assert" | |
| "networkteam.com/cqrs-test/valueobjects" | |
| ) | |
| func TestNewNodeIdentifier_Empty(t *testing.T) { | |
| _, err := valueobjects.NewNodeIdentifier("") | |
| assert.Error(t, err) | |
| } | |
| func TestNewNodeIdentifier_Valid(t *testing.T) { | |
| v, err := valueobjects.NewNodeIdentifier("d20d4118-a28b-11e7-8f0d-777ee5502250") | |
| assert.NoError(t, err) | |
| assert.Equal(t, "d20d4118-a28b-11e7-8f0d-777ee5502250", v.Identifier()) | |
| } | |
| func TestNodeIdentifier_JsonSerialize(t *testing.T) { | |
| v, err := valueobjects.NewNodeIdentifier("d20d4118-a28b-11e7-8f0d-777ee5502250") | |
| assert.NoError(t, err) | |
| b, err := json.Marshal(v) | |
| assert.NoError(t, err) | |
| assert.JSONEq(t, `{"identifier":"d20d4118-a28b-11e7-8f0d-777ee5502250"}`, string(b)) | |
| } | |
| func TestNodeIdentifier_JsonUnserialize_Valid(t *testing.T) { | |
| var v valueobjects.NodeIdentifier | |
| var data = []byte(`{"identifier":"d20d4118-a28b-11e7-8f0d-777ee5502250"}`) | |
| err := json.Unmarshal(data, &v) | |
| assert.NoError(t, err) | |
| assert.Equal(t, "d20d4118-a28b-11e7-8f0d-777ee5502250", v.Identifier()) | |
| } | |
| func TestNodeIdentifier_JsonUnserialize_Invalid(t *testing.T) { | |
| var v valueobjects.NodeIdentifier | |
| var data = []byte(`{"identifier":""}`) | |
| err := json.Unmarshal(data, &v) | |
| assert.Error(t, err) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment