even more refactoring
This commit is contained in:
@@ -67,8 +67,8 @@ func (g *RemoteGrainGRPC) GetId() CartId {
|
||||
return g.Id
|
||||
}
|
||||
|
||||
// Apply executes a cart mutation remotely using the typed oneof MutationEnvelope
|
||||
// and returns a *CartGrain reconstructed from the typed MutationReply (state oneof).
|
||||
// Apply executes a cart mutation via per-mutation RPCs (breaking v2 API)
|
||||
// and returns a *CartGrain reconstructed from the CartMutationReply state.
|
||||
func (g *RemoteGrainGRPC) Apply(content interface{}, isReplay bool) (*CartGrain, error) {
|
||||
if isReplay {
|
||||
return nil, fmt.Errorf("replay not supported for remote grains")
|
||||
@@ -78,45 +78,100 @@ func (g *RemoteGrainGRPC) Apply(content interface{}, isReplay bool) (*CartGrain,
|
||||
}
|
||||
|
||||
ts := time.Now().Unix()
|
||||
env := &proto.MutationEnvelope{
|
||||
CartId: g.Id.String(),
|
||||
ClientTimestamp: ts,
|
||||
}
|
||||
|
||||
var invoke func(ctx context.Context) (*proto.CartMutationReply, error)
|
||||
|
||||
switch m := content.(type) {
|
||||
case *proto.AddRequest:
|
||||
env.Mutation = &proto.MutationEnvelope_AddRequest{AddRequest: m}
|
||||
invoke = func(ctx context.Context) (*proto.CartMutationReply, error) {
|
||||
return g.client.AddRequest(ctx, &proto.AddRequestRequest{
|
||||
CartId: g.Id.String(),
|
||||
ClientTimestamp: ts,
|
||||
Payload: m,
|
||||
})
|
||||
}
|
||||
case *proto.AddItem:
|
||||
env.Mutation = &proto.MutationEnvelope_AddItem{AddItem: m}
|
||||
invoke = func(ctx context.Context) (*proto.CartMutationReply, error) {
|
||||
return g.client.AddItem(ctx, &proto.AddItemRequest{
|
||||
CartId: g.Id.String(),
|
||||
ClientTimestamp: ts,
|
||||
Payload: m,
|
||||
})
|
||||
}
|
||||
case *proto.RemoveItem:
|
||||
env.Mutation = &proto.MutationEnvelope_RemoveItem{RemoveItem: m}
|
||||
invoke = func(ctx context.Context) (*proto.CartMutationReply, error) {
|
||||
return g.client.RemoveItem(ctx, &proto.RemoveItemRequest{
|
||||
CartId: g.Id.String(),
|
||||
ClientTimestamp: ts,
|
||||
Payload: m,
|
||||
})
|
||||
}
|
||||
case *proto.RemoveDelivery:
|
||||
env.Mutation = &proto.MutationEnvelope_RemoveDelivery{RemoveDelivery: m}
|
||||
invoke = func(ctx context.Context) (*proto.CartMutationReply, error) {
|
||||
return g.client.RemoveDelivery(ctx, &proto.RemoveDeliveryRequest{
|
||||
CartId: g.Id.String(),
|
||||
ClientTimestamp: ts,
|
||||
Payload: m,
|
||||
})
|
||||
}
|
||||
case *proto.ChangeQuantity:
|
||||
env.Mutation = &proto.MutationEnvelope_ChangeQuantity{ChangeQuantity: m}
|
||||
invoke = func(ctx context.Context) (*proto.CartMutationReply, error) {
|
||||
return g.client.ChangeQuantity(ctx, &proto.ChangeQuantityRequest{
|
||||
CartId: g.Id.String(),
|
||||
ClientTimestamp: ts,
|
||||
Payload: m,
|
||||
})
|
||||
}
|
||||
case *proto.SetDelivery:
|
||||
env.Mutation = &proto.MutationEnvelope_SetDelivery{SetDelivery: m}
|
||||
invoke = func(ctx context.Context) (*proto.CartMutationReply, error) {
|
||||
return g.client.SetDelivery(ctx, &proto.SetDeliveryRequest{
|
||||
CartId: g.Id.String(),
|
||||
ClientTimestamp: ts,
|
||||
Payload: m,
|
||||
})
|
||||
}
|
||||
case *proto.SetPickupPoint:
|
||||
env.Mutation = &proto.MutationEnvelope_SetPickupPoint{SetPickupPoint: m}
|
||||
invoke = func(ctx context.Context) (*proto.CartMutationReply, error) {
|
||||
return g.client.SetPickupPoint(ctx, &proto.SetPickupPointRequest{
|
||||
CartId: g.Id.String(),
|
||||
ClientTimestamp: ts,
|
||||
Payload: m,
|
||||
})
|
||||
}
|
||||
case *proto.CreateCheckoutOrder:
|
||||
env.Mutation = &proto.MutationEnvelope_CreateCheckoutOrder{CreateCheckoutOrder: m}
|
||||
return nil, fmt.Errorf("CreateCheckoutOrder deprecated: checkout is handled via HTTP endpoint (HandleCheckout)")
|
||||
case *proto.SetCartRequest:
|
||||
env.Mutation = &proto.MutationEnvelope_SetCartItems{SetCartItems: m}
|
||||
invoke = func(ctx context.Context) (*proto.CartMutationReply, error) {
|
||||
return g.client.SetCartItems(ctx, &proto.SetCartItemsRequest{
|
||||
CartId: g.Id.String(),
|
||||
ClientTimestamp: ts,
|
||||
Payload: m,
|
||||
})
|
||||
}
|
||||
case *proto.OrderCreated:
|
||||
env.Mutation = &proto.MutationEnvelope_OrderCompleted{OrderCompleted: m}
|
||||
invoke = func(ctx context.Context) (*proto.CartMutationReply, error) {
|
||||
return g.client.OrderCompleted(ctx, &proto.OrderCompletedRequest{
|
||||
CartId: g.Id.String(),
|
||||
ClientTimestamp: ts,
|
||||
Payload: m,
|
||||
})
|
||||
}
|
||||
default:
|
||||
return nil, fmt.Errorf("unsupported mutation type %T", content)
|
||||
}
|
||||
|
||||
if invoke == nil {
|
||||
return nil, fmt.Errorf("no invocation mapped for mutation %T", content)
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), g.mutateTimeout)
|
||||
defer cancel()
|
||||
|
||||
resp, err := g.client.Mutate(ctx, env)
|
||||
resp, err := invoke(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Map typed reply
|
||||
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
||||
if e := resp.GetError(); e != "" {
|
||||
return nil, fmt.Errorf("remote mutation failed %d: %s", resp.StatusCode, e)
|
||||
|
||||
Reference in New Issue
Block a user