docs: reorder & testing
This commit is contained in:
+204
-98
@@ -5,109 +5,12 @@ import (
|
||||
"fmt"
|
||||
|
||||
model "galaxy/model/order"
|
||||
commonfbs "galaxy/schema/fbs/common"
|
||||
fbs "galaxy/schema/fbs/order"
|
||||
|
||||
flatbuffers "github.com/google/flatbuffers/go"
|
||||
)
|
||||
|
||||
// OrderToPayload converts model.Order from the internal representation to
|
||||
// FlatBuffers bytes that can be sent over network transports.
|
||||
//
|
||||
// The function returns an error when the input contains unsupported command
|
||||
// types or values that cannot be represented by the current FlatBuffers schema.
|
||||
func OrderToPayload(o *model.Order) ([]byte, error) {
|
||||
if o == nil {
|
||||
return nil, errors.New("encode order payload: order is nil")
|
||||
}
|
||||
|
||||
builder := flatbuffers.NewBuilder(1024)
|
||||
commandOffsets := make([]flatbuffers.UOffsetT, len(o.Commands))
|
||||
|
||||
for i := range o.Commands {
|
||||
encoded, err := encodeOrderCommand(builder, o.Commands[i], i)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
cmdID := builder.CreateString(encoded.cmdID)
|
||||
|
||||
fbs.CommandItemStart(builder)
|
||||
fbs.CommandItemAddCmdId(builder, cmdID)
|
||||
if encoded.cmdApplied != nil {
|
||||
fbs.CommandItemAddCmdApplied(builder, *encoded.cmdApplied)
|
||||
}
|
||||
if encoded.cmdErrCode != nil {
|
||||
fbs.CommandItemAddCmdErrorCode(builder, int64(*encoded.cmdErrCode))
|
||||
}
|
||||
fbs.CommandItemAddPayloadType(builder, encoded.payloadType)
|
||||
fbs.CommandItemAddPayload(builder, encoded.payloadOffset)
|
||||
commandOffsets[i] = fbs.CommandItemEnd(builder)
|
||||
}
|
||||
|
||||
var commandsVector flatbuffers.UOffsetT
|
||||
if len(commandOffsets) > 0 {
|
||||
fbs.OrderStartCommandsVector(builder, len(commandOffsets))
|
||||
for i := len(commandOffsets) - 1; i >= 0; i-- {
|
||||
builder.PrependUOffsetT(commandOffsets[i])
|
||||
}
|
||||
commandsVector = builder.EndVector(len(commandOffsets))
|
||||
}
|
||||
|
||||
fbs.OrderStart(builder)
|
||||
fbs.OrderAddUpdatedAt(builder, int64(o.UpdatedAt))
|
||||
if len(commandOffsets) > 0 {
|
||||
fbs.OrderAddCommands(builder, commandsVector)
|
||||
}
|
||||
orderOffset := fbs.OrderEnd(builder)
|
||||
fbs.FinishOrderBuffer(builder, orderOffset)
|
||||
|
||||
return builder.FinishedBytes(), nil
|
||||
}
|
||||
|
||||
// PayloadToOrder converts FlatBuffers payload bytes into model.Order.
|
||||
//
|
||||
// The function validates payload structure, command payload type, enum values,
|
||||
// and integer conversions. Malformed payloads are returned as errors.
|
||||
func PayloadToOrder(data []byte) (result *model.Order, err error) {
|
||||
if len(data) == 0 {
|
||||
return nil, errors.New("decode order payload: data is empty")
|
||||
}
|
||||
|
||||
defer func() {
|
||||
if recovered := recover(); recovered != nil {
|
||||
result = nil
|
||||
err = fmt.Errorf("decode order payload: panic recovered: %v", recovered)
|
||||
}
|
||||
}()
|
||||
|
||||
flatOrder := fbs.GetRootAsOrder(data, 0)
|
||||
updatedAt, err := int64ToInt(flatOrder.UpdatedAt(), "updated_at")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("decode order payload: %w", err)
|
||||
}
|
||||
|
||||
result = &model.Order{UpdatedAt: updatedAt}
|
||||
commandsLen := flatOrder.CommandsLength()
|
||||
if commandsLen > 0 {
|
||||
result.Commands = make([]model.DecodableCommand, commandsLen)
|
||||
}
|
||||
|
||||
flatCommand := new(fbs.CommandItem)
|
||||
for i := 0; i < commandsLen; i++ {
|
||||
if !flatOrder.Commands(flatCommand, i) {
|
||||
return nil, fmt.Errorf("decode order command %d: command item is missing", i)
|
||||
}
|
||||
|
||||
command, err := decodeOrderCommand(flatCommand, i)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result.Commands[i] = command
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
type encodedCommand struct {
|
||||
cmdID string
|
||||
cmdApplied *bool
|
||||
@@ -701,6 +604,10 @@ func decodeOrderCommand(flatCommand *fbs.CommandItem, index int) (model.Decodabl
|
||||
}
|
||||
}
|
||||
|
||||
// int64ToInt narrows v to a Go int. Returns an error when v overflows
|
||||
// the platform `int` range (only possible on 32-bit builds; on 64-bit
|
||||
// the check is a no-op). fieldName is used in the error for caller
|
||||
// context.
|
||||
func int64ToInt(value int64, field string) (int, error) {
|
||||
maxInt := int64(int(^uint(0) >> 1))
|
||||
minInt := -maxInt - 1
|
||||
@@ -897,3 +804,202 @@ func cloneIntPointer(value *int) *int {
|
||||
cloned := *value
|
||||
return &cloned
|
||||
}
|
||||
|
||||
// UserGamesCommandToPayload converts model.UserGamesCommand to
|
||||
// FlatBuffers bytes suitable for the authenticated gateway transport.
|
||||
// `GameID` is required.
|
||||
func UserGamesCommandToPayload(req *model.UserGamesCommand) ([]byte, error) {
|
||||
if req == nil {
|
||||
return nil, errors.New("encode user games command payload: request is nil")
|
||||
}
|
||||
|
||||
builder := flatbuffers.NewBuilder(1024)
|
||||
commandsVec, err := encodeCommandItemVector(builder, req.Commands, "user games command")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
fbs.UserGamesCommandStart(builder)
|
||||
hi, lo := uuidToHiLo(req.GameID)
|
||||
fbs.UserGamesCommandAddGameId(builder, commonfbs.CreateUUID(builder, hi, lo))
|
||||
if commandsVec != 0 {
|
||||
fbs.UserGamesCommandAddCommands(builder, commandsVec)
|
||||
}
|
||||
offset := fbs.UserGamesCommandEnd(builder)
|
||||
fbs.FinishUserGamesCommandBuffer(builder, offset)
|
||||
|
||||
return builder.FinishedBytes(), nil
|
||||
}
|
||||
|
||||
// PayloadToUserGamesCommand converts FlatBuffers payload bytes into
|
||||
// model.UserGamesCommand.
|
||||
func PayloadToUserGamesCommand(data []byte) (result *model.UserGamesCommand, err error) {
|
||||
if len(data) == 0 {
|
||||
return nil, errors.New("decode user games command payload: data is empty")
|
||||
}
|
||||
|
||||
defer func() {
|
||||
if recovered := recover(); recovered != nil {
|
||||
result = nil
|
||||
err = fmt.Errorf("decode user games command payload: panic recovered: %v", recovered)
|
||||
}
|
||||
}()
|
||||
|
||||
flat := fbs.GetRootAsUserGamesCommand(data, 0)
|
||||
gameID := flat.GameId(nil)
|
||||
if gameID == nil {
|
||||
return nil, errors.New("decode user games command payload: game_id is missing")
|
||||
}
|
||||
out := &model.UserGamesCommand{
|
||||
GameID: uuidFromHiLo(gameID.Hi(), gameID.Lo()),
|
||||
}
|
||||
count := flat.CommandsLength()
|
||||
if count > 0 {
|
||||
out.Commands = make([]model.DecodableCommand, count)
|
||||
flatCommand := new(fbs.CommandItem)
|
||||
for i := 0; i < count; i++ {
|
||||
if !flat.Commands(flatCommand, i) {
|
||||
return nil, fmt.Errorf("decode user games command %d: command item is missing", i)
|
||||
}
|
||||
cmd, decodeErr := decodeOrderCommand(flatCommand, i)
|
||||
if decodeErr != nil {
|
||||
return nil, decodeErr
|
||||
}
|
||||
out.Commands[i] = cmd
|
||||
}
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// UserGamesOrderToPayload converts model.UserGamesOrder to FlatBuffers
|
||||
// bytes suitable for the authenticated gateway transport.
|
||||
func UserGamesOrderToPayload(req *model.UserGamesOrder) ([]byte, error) {
|
||||
if req == nil {
|
||||
return nil, errors.New("encode user games order payload: request is nil")
|
||||
}
|
||||
|
||||
builder := flatbuffers.NewBuilder(1024)
|
||||
commandsVec, err := encodeCommandItemVector(builder, req.Commands, "user games order")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
fbs.UserGamesOrderStart(builder)
|
||||
hi, lo := uuidToHiLo(req.GameID)
|
||||
fbs.UserGamesOrderAddGameId(builder, commonfbs.CreateUUID(builder, hi, lo))
|
||||
fbs.UserGamesOrderAddUpdatedAt(builder, int64(req.UpdatedAt))
|
||||
if commandsVec != 0 {
|
||||
fbs.UserGamesOrderAddCommands(builder, commandsVec)
|
||||
}
|
||||
offset := fbs.UserGamesOrderEnd(builder)
|
||||
fbs.FinishUserGamesOrderBuffer(builder, offset)
|
||||
|
||||
return builder.FinishedBytes(), nil
|
||||
}
|
||||
|
||||
// PayloadToUserGamesOrder converts FlatBuffers payload bytes into
|
||||
// model.UserGamesOrder.
|
||||
func PayloadToUserGamesOrder(data []byte) (result *model.UserGamesOrder, err error) {
|
||||
if len(data) == 0 {
|
||||
return nil, errors.New("decode user games order payload: data is empty")
|
||||
}
|
||||
|
||||
defer func() {
|
||||
if recovered := recover(); recovered != nil {
|
||||
result = nil
|
||||
err = fmt.Errorf("decode user games order payload: panic recovered: %v", recovered)
|
||||
}
|
||||
}()
|
||||
|
||||
flat := fbs.GetRootAsUserGamesOrder(data, 0)
|
||||
gameID := flat.GameId(nil)
|
||||
if gameID == nil {
|
||||
return nil, errors.New("decode user games order payload: game_id is missing")
|
||||
}
|
||||
updatedAt, convErr := int64ToInt(flat.UpdatedAt(), "updated_at")
|
||||
if convErr != nil {
|
||||
return nil, fmt.Errorf("decode user games order payload: %w", convErr)
|
||||
}
|
||||
out := &model.UserGamesOrder{
|
||||
GameID: uuidFromHiLo(gameID.Hi(), gameID.Lo()),
|
||||
UpdatedAt: updatedAt,
|
||||
}
|
||||
count := flat.CommandsLength()
|
||||
if count > 0 {
|
||||
out.Commands = make([]model.DecodableCommand, count)
|
||||
flatCommand := new(fbs.CommandItem)
|
||||
for i := 0; i < count; i++ {
|
||||
if !flat.Commands(flatCommand, i) {
|
||||
return nil, fmt.Errorf("decode user games order %d: command item is missing", i)
|
||||
}
|
||||
cmd, decodeErr := decodeOrderCommand(flatCommand, i)
|
||||
if decodeErr != nil {
|
||||
return nil, decodeErr
|
||||
}
|
||||
out.Commands[i] = cmd
|
||||
}
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// EmptyUserGamesCommandResponsePayload returns a FlatBuffers-encoded
|
||||
// empty `UserGamesCommandResponse` buffer. Used by gateway to ack a
|
||||
// successful `MessageTypeUserGamesCommand` even though the engine
|
||||
// returns 204 No Content — the typed envelope keeps the message-type
|
||||
// contract symmetric with other authenticated routes.
|
||||
func EmptyUserGamesCommandResponsePayload() []byte {
|
||||
builder := flatbuffers.NewBuilder(16)
|
||||
fbs.UserGamesCommandResponseStart(builder)
|
||||
offset := fbs.UserGamesCommandResponseEnd(builder)
|
||||
fbs.FinishUserGamesCommandResponseBuffer(builder, offset)
|
||||
return builder.FinishedBytes()
|
||||
}
|
||||
|
||||
// EmptyUserGamesOrderResponsePayload mirrors
|
||||
// EmptyUserGamesCommandResponsePayload for `MessageTypeUserGamesOrder`.
|
||||
func EmptyUserGamesOrderResponsePayload() []byte {
|
||||
builder := flatbuffers.NewBuilder(16)
|
||||
fbs.UserGamesOrderResponseStart(builder)
|
||||
offset := fbs.UserGamesOrderResponseEnd(builder)
|
||||
fbs.FinishUserGamesOrderResponseBuffer(builder, offset)
|
||||
return builder.FinishedBytes()
|
||||
}
|
||||
|
||||
// encodeCommandItemVector serialises a slice of DecodableCommand into a
|
||||
// FlatBuffers vector of CommandItem. Used by UserGamesCommandToPayload
|
||||
// and UserGamesOrderToPayload to keep the per-command encoding logic in
|
||||
// one place.
|
||||
func encodeCommandItemVector(builder *flatbuffers.Builder, commands []model.DecodableCommand, opLabel string) (flatbuffers.UOffsetT, error) {
|
||||
offsets := make([]flatbuffers.UOffsetT, len(commands))
|
||||
for i := range commands {
|
||||
encoded, err := encodeOrderCommand(builder, commands[i], i)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("encode %s: %w", opLabel, err)
|
||||
}
|
||||
cmdID := builder.CreateString(encoded.cmdID)
|
||||
fbs.CommandItemStart(builder)
|
||||
fbs.CommandItemAddCmdId(builder, cmdID)
|
||||
if encoded.cmdApplied != nil {
|
||||
fbs.CommandItemAddCmdApplied(builder, *encoded.cmdApplied)
|
||||
}
|
||||
if encoded.cmdErrCode != nil {
|
||||
fbs.CommandItemAddCmdErrorCode(builder, int64(*encoded.cmdErrCode))
|
||||
}
|
||||
fbs.CommandItemAddPayloadType(builder, encoded.payloadType)
|
||||
fbs.CommandItemAddPayload(builder, encoded.payloadOffset)
|
||||
offsets[i] = fbs.CommandItemEnd(builder)
|
||||
}
|
||||
if len(offsets) == 0 {
|
||||
return 0, nil
|
||||
}
|
||||
// `UserGamesCommandStartCommandsVector` and the corresponding
|
||||
// `UserGamesOrderStartCommandsVector` are identical helpers (both
|
||||
// expand to `builder.StartVector(4, numElems, 4)`); we use the
|
||||
// command flavour for both message types so the helper has a
|
||||
// single dependency point.
|
||||
fbs.UserGamesCommandStartCommandsVector(builder, len(offsets))
|
||||
for i := len(offsets) - 1; i >= 0; i-- {
|
||||
builder.PrependUOffsetT(offsets[i])
|
||||
}
|
||||
return builder.EndVector(len(offsets)), nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user