-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update serverservice for toolbox/events v0.6.0 (#214)
- Loading branch information
Showing
6 changed files
with
122 additions
and
17 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
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
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,61 @@ | ||
//nolint:wsl,revive | ||
package serverservice | ||
|
||
import ( | ||
"encoding/json" | ||
"time" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/volatiletech/null/v8" | ||
|
||
"go.hollow.sh/serverservice/internal/models" | ||
) | ||
|
||
var ( | ||
ErrNilServer = errors.New("bogus server structure provided") | ||
ErrBadJSONOut = errors.New("object serializaion failed") | ||
ErrBadJSONIn = errors.New("object deserializaion failed") | ||
) | ||
|
||
// MsgMetadata captures some message-type agnostic descriptive data a consumer might need | ||
type MsgMetadata struct { | ||
CreatedAt time.Time `json:"created_at,omitempty"` | ||
UpdatedAt time.Time `json:"updated_at,omitempty"` | ||
} | ||
|
||
// CreateServer is a message type published via NATS | ||
type CreateServer struct { | ||
Metadata *MsgMetadata `json:"metadata,omitempty"` | ||
Name null.String `json:"name"` | ||
FacilityCode null.String `json:"facility_code"` | ||
ID string `json:"id"` | ||
} | ||
|
||
// NewCreateServerMessage composes a CreateServer message for NATS | ||
func NewCreateServerMessage(srv *models.Server) ([]byte, error) { | ||
if srv == nil { | ||
return nil, ErrNilServer | ||
} | ||
cs := &CreateServer{ | ||
Metadata: &MsgMetadata{ | ||
CreatedAt: time.Now(), | ||
}, | ||
Name: srv.Name, | ||
FacilityCode: srv.FacilityCode, | ||
ID: srv.ID, | ||
} | ||
byt, err := json.Marshal(cs) | ||
if err != nil { | ||
return nil, errors.Wrap(ErrBadJSONOut, err.Error()) | ||
} | ||
return byt, err | ||
} | ||
|
||
// DeserializeCreateServer reconstitutes a CreateServer from raw bytes | ||
func DeserializeCreateServer(inc []byte) (*CreateServer, error) { | ||
cs := &CreateServer{} | ||
if err := json.Unmarshal(inc, cs); err != nil { | ||
return nil, errors.Wrap(ErrBadJSONIn, err.Error()) | ||
} | ||
return cs, 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,42 @@ | ||
//nolint:wsl | ||
package serverservice | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"github.com/volatiletech/null/v8" | ||
|
||
"go.hollow.sh/serverservice/internal/models" | ||
) | ||
|
||
func TestSerialization(t *testing.T) { | ||
srv := &models.Server{ | ||
Name: null.StringFrom("server-name"), | ||
FacilityCode: null.StringFrom("fc13"), | ||
ID: "some-uuid-str", | ||
} | ||
|
||
_, err := NewCreateServerMessage((*models.Server)(nil)) | ||
require.ErrorIs(t, err, ErrNilServer, "nil input") | ||
|
||
byt, err := NewCreateServerMessage(srv) | ||
require.NoError(t, err, "good server obj") | ||
require.NotNil(t, byt, "good server obj") | ||
|
||
bogus := []byte("bogus") | ||
_, err = DeserializeCreateServer(bogus) | ||
require.ErrorIs(t, err, ErrBadJSONIn, "bogus deserialize") | ||
|
||
exp := &CreateServer{ | ||
Name: null.StringFrom("server-name"), | ||
FacilityCode: null.StringFrom("fc13"), | ||
ID: "some-uuid-str", | ||
} | ||
|
||
cs, err := DeserializeCreateServer(byt) | ||
require.NoError(t, err, "good deserialize") | ||
require.Equal(t, exp.Name, cs.Name, "good deserialize name") | ||
require.Equal(t, exp.FacilityCode, cs.FacilityCode, "good deserialize facility") | ||
require.Equal(t, exp.ID, cs.ID, "good deserialize id") | ||
} |
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
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