Switch to thrift binary serialization (#392)

This commit is contained in:
Alexey 2019-12-04 13:30:17 +03:00 committed by GitHub
parent bf817253ea
commit e3ed327105
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 33 additions and 532 deletions

View File

@ -715,19 +715,15 @@ assert_shop_operable(Shop) ->
-spec marshal_event_payload([customer_change()]) ->
hg_machine:event_payload().
marshal_event_payload(Changes) ->
%@TODO MIGRATION: To thift binaries
%wrap_event_payload({customer_changes, Changes}).
#{format_version => undefined, data => marshal({list, change}, Changes)}.
wrap_event_payload({customer_changes, Changes}).
%@TODO MIGRATION: To thift binaries
%wrap_event_payload(Payload) ->
% Type = {struct, union, {dmsl_payment_processing_thrift, 'EventPayload'}},
% Bin = hg_proto_utils:serialize(Type, Payload),
% #{
% format_version => 1,
% data => {bin, Bin}
% }.
wrap_event_payload(Payload) ->
Type = {struct, union, {dmsl_payment_processing_thrift, 'EventPayload'}},
Bin = hg_proto_utils:serialize(Type, Payload),
#{
format_version => 1,
data => {bin, Bin}
}.
-spec marshal_customer_params(customer_params()) ->
binary().
@ -735,191 +731,6 @@ marshal_customer_params(Params) ->
Type = {struct, struct, {dmsl_payment_processing_thrift, 'CustomerParams'}},
hg_proto_utils:serialize(Type, Params).
marshal({list, T}, Vs) when is_list(Vs) ->
[marshal(T, V) || V <- Vs];
%% Changes
marshal(change, Change) ->
marshal(change_payload, Change);
marshal(change_payload, ?customer_created(CustomerID, OwnerID, ShopID, Metadata, ContactInfo, CreatedAt)) ->
[2, #{
<<"change">> => <<"created">>,
<<"customer_id">> => marshal(str , CustomerID),
<<"owner_id">> => marshal(str , OwnerID),
<<"shop_id">> => marshal(str , ShopID),
<<"metadata">> => marshal(metadata , Metadata),
<<"contact_info">> => marshal(contact_info, ContactInfo),
<<"created_at">> => marshal(str , CreatedAt)
}];
marshal(change_payload, ?customer_deleted()) ->
[1, #{
<<"change">> => <<"deleted">>
}];
marshal(change_payload, ?customer_status_changed(CustomerStatus)) ->
[1, #{
<<"change">> => <<"status">>,
<<"status">> => marshal(customer_status, CustomerStatus)
}];
marshal(change_payload, ?customer_binding_changed(CustomerBindingID, Payload)) ->
[1, #{
<<"change">> => <<"binding">>,
<<"binding_id">> => marshal(str, CustomerBindingID),
<<"payload">> => marshal(binding_change_payload, Payload)
}];
%% Change components
marshal(
customer,
#payproc_Customer{
id = ID,
owner_id = OwnerID,
shop_id = ShopID,
created_at = CreatedAt,
contact_info = ContactInfo,
metadata = Metadata
}
) ->
#{
<<"id">> => marshal(str , ID),
<<"owner_id">> => marshal(str , OwnerID),
<<"shop_id">> => marshal(str , ShopID),
<<"created_at">> => marshal(str , CreatedAt),
<<"contact_info">> => marshal(contact_info, ContactInfo),
<<"metadata">> => marshal(metadata , Metadata)
};
marshal(customer_status, ?customer_unready()) ->
<<"unready">>;
marshal(customer_status, ?customer_ready()) ->
<<"ready">>;
marshal(
binding,
#payproc_CustomerBinding{
id = ID,
rec_payment_tool_id = RecPaymentToolID,
payment_resource = PaymentResource,
status = Status,
party_revision = PartyRevision,
domain_revision = DomainRevision
}
) ->
#{
<<"id">> => marshal(str , ID),
<<"recpaytool_id">> => marshal(str , RecPaymentToolID),
<<"payresource">> => marshal(payment_resource , PaymentResource),
<<"status">> => marshal(binding_status , Status),
<<"party_revision">> => marshal(int , PartyRevision),
<<"domain_revision">> => marshal(int , DomainRevision)
};
marshal(
contact_info,
#domain_ContactInfo{
phone_number = PhoneNumber,
email = Email
}
) ->
genlib_map:compact(#{
<<"phone">> => marshal(str, PhoneNumber),
<<"email">> => marshal(str, Email)
});
marshal(
payment_resource,
#domain_DisposablePaymentResource{
payment_tool = PaymentTool,
payment_session_id = PaymentSessionID,
client_info = ClientInfo
}
) ->
#{
<<"paytool">> => hg_payment_tool:marshal(PaymentTool),
<<"session">> => marshal(str , PaymentSessionID),
<<"client_info">> => marshal(client_info , ClientInfo)
};
marshal(
client_info,
#domain_ClientInfo{
ip_address = IPAddress,
fingerprint = Fingerprint
}
) ->
genlib_map:compact(#{
<<"ip">> => marshal(str, IPAddress),
<<"fingerprint">> => marshal(str, Fingerprint)
});
marshal(binding_status, ?customer_binding_pending()) ->
?BINARY_BINDING_STATUS_PENDING;
marshal(binding_status, ?customer_binding_succeeded()) ->
?BINARY_BINDING_STATUS_SUCCEEDED;
marshal(binding_status, ?customer_binding_failed(Failure)) ->
?BINARY_BINDING_STATUS_FAILED(
marshal(failure, Failure)
);
marshal(binding_change_payload, ?customer_binding_started(CustomerBinding, Timestamp)) ->
[
<<"started">>,
marshal(binding, CustomerBinding),
marshal(str, Timestamp)
];
marshal(binding_change_payload, ?customer_binding_status_changed(CustomerBindingStatus)) ->
[
<<"status">>,
marshal(binding_status, CustomerBindingStatus)
];
marshal(binding_change_payload, ?customer_binding_interaction_requested(UserInteraction)) ->
[
<<"interaction">>,
marshal(interaction, UserInteraction)
];
marshal(interaction, {redirect, Redirect}) ->
[
<<"redirect">>,
marshal(redirect, Redirect)
];
marshal(redirect, {get_request, #'BrowserGetRequest'{uri = URI}}) ->
[
<<"get">>,
marshal(str, URI)
];
marshal(redirect, {post_request, #'BrowserPostRequest'{uri = URI, form = Form}}) ->
[
<<"post">>,
#{
<<"uri">> => marshal(str, URI),
<<"form">> => marshal(map_str, Form)
}
];
marshal(sub_failure, undefined) ->
undefined;
marshal(sub_failure, #domain_SubFailure{} = SubFailure) ->
genlib_map:compact(#{
<<"code">> => marshal(str , SubFailure#domain_SubFailure.code),
<<"sub" >> => marshal(sub_failure, SubFailure#domain_SubFailure.sub )
});
marshal(failure, {operation_timeout, _}) ->
[1, <<"operation_timeout">>];
marshal(failure, {failure, #domain_Failure{} = Failure}) ->
[1, [<<"failure">>, genlib_map:compact(#{
<<"code" >> => marshal(str , Failure#domain_Failure.code ),
<<"reason">> => marshal(str , Failure#domain_Failure.reason),
<<"sub" >> => marshal(sub_failure, Failure#domain_Failure.sub )
})]];
marshal(metadata, Metadata) ->
hg_msgpack_marshalling:marshal(json, Metadata);
%% AuxState
marshal(auxst, AuxState) ->

View File

@ -333,104 +333,15 @@ marchal_invoice_template_params(Params) ->
-spec marshal_event_payload([invoice_template_change()]) ->
hg_machine:event_payload().
marshal_event_payload(Changes) when is_list(Changes) ->
#{format_version => undefined, data => [marshal(change, Change) || Change <- Changes]}.
%@TODO MIGRATION: To thift binaries
%wrap_event_payload({invoice_template_changes, Changes}).
wrap_event_payload({invoice_template_changes, Changes}).
%@TODO MIGRATION: To thift binaries
%wrap_event_payload(Payload) ->
% Type = {struct, union, {dmsl_payment_processing_thrift, 'EventPayload'}},
% Bin = hg_proto_utils:serialize(Type, Payload),
% #{
% format_version => 1,
% data => {bin, Bin}
% }.
marshal(change, ?tpl_created(InvoiceTpl)) ->
[3, #{
<<"change">> => <<"created">>,
<<"tpl">> => marshal(invoice_template, InvoiceTpl)
}];
marshal(change, ?tpl_updated(Diff)) ->
[3, #{
<<"change">> => <<"updated">>,
<<"diff">> => marshal(invoice_template_diff, Diff)
}];
marshal(change, ?tpl_deleted()) ->
[3, #{
<<"change">> => <<"deleted">>
}];
marshal(invoice_template, #domain_InvoiceTemplate{} = InvoiceTpl) ->
genlib_map:compact(#{
<<"id">> => marshal(str, InvoiceTpl#domain_InvoiceTemplate.id),
<<"shop_id">> => marshal(str, InvoiceTpl#domain_InvoiceTemplate.shop_id),
<<"owner_id">> => marshal(str, InvoiceTpl#domain_InvoiceTemplate.owner_id),
<<"lifetime">> => marshal(lifetime, InvoiceTpl#domain_InvoiceTemplate.invoice_lifetime),
<<"product">> => marshal(str, InvoiceTpl#domain_InvoiceTemplate.product),
<<"description">> => marshal(str, InvoiceTpl#domain_InvoiceTemplate.description),
<<"details">> => marshal(details, InvoiceTpl#domain_InvoiceTemplate.details),
<<"context">> => hg_content:marshal(InvoiceTpl#domain_InvoiceTemplate.context)
});
marshal(invoice_template_diff, #payproc_InvoiceTemplateUpdateParams{} = Diff) ->
genlib_map:compact(#{
<<"lifetime">> => marshal(lifetime, Diff#payproc_InvoiceTemplateUpdateParams.invoice_lifetime),
<<"product">> => marshal(str, Diff#payproc_InvoiceTemplateUpdateParams.product),
<<"description">> => marshal(str, Diff#payproc_InvoiceTemplateUpdateParams.description),
<<"details">> => marshal(details, Diff#payproc_InvoiceTemplateUpdateParams.details),
<<"context">> => hg_content:marshal(Diff#payproc_InvoiceTemplateUpdateParams.context)
});
marshal(details, {cart, #domain_InvoiceCart{} = Cart}) ->
[<<"cart">>, marshal(cart, Cart)];
marshal(details, {product, #domain_InvoiceTemplateProduct{} = Product}) ->
[<<"template_product">>, marshal(template_product, Product)];
marshal(cart, #domain_InvoiceCart{lines = Lines}) ->
#{<<"lines">> => [marshal(line, Line) || Line <- Lines]};
marshal(line, #domain_InvoiceLine{} = InvoiceLine) ->
#{
<<"product">> => marshal(str, InvoiceLine#domain_InvoiceLine.product),
<<"quantity">> => marshal(int, InvoiceLine#domain_InvoiceLine.quantity),
<<"price">> => hg_cash:marshal(InvoiceLine#domain_InvoiceLine.price),
<<"metadata">> => marshal(metadata, InvoiceLine#domain_InvoiceLine.metadata)
};
marshal(template_product, #domain_InvoiceTemplateProduct{} = Product) ->
#{
<<"product">> => marshal(str, Product#domain_InvoiceTemplateProduct.product),
<<"price">> => marshal(cost, Product#domain_InvoiceTemplateProduct.price),
<<"metadata">> => marshal(metadata, Product#domain_InvoiceTemplateProduct.metadata)
};
marshal(lifetime, #domain_LifetimeInterval{years = Years, months = Months, days = Days}) ->
genlib_map:compact(#{
<<"years">> => marshal(int, Years),
<<"months">> => marshal(int, Months),
<<"days">> => marshal(int, Days)
});
marshal(cost, {fixed, Cash}) ->
[<<"fixed">>, hg_cash:marshal(Cash)];
marshal(cost, {range, CashRange}) ->
[<<"range">>, hg_cash_range:marshal(CashRange)];
marshal(cost, {unlim, _}) ->
<<"unlim">>;
marshal(metadata, Metadata) ->
maps:fold(
fun(K, V, Acc) ->
maps:put(marshal(str, K), hg_msgpack_marshalling:unmarshal(V), Acc)
end,
#{},
Metadata
);
marshal(_, Other) ->
Other.
wrap_event_payload(Payload) ->
Type = {struct, union, {dmsl_payment_processing_thrift, 'EventPayload'}},
Bin = hg_proto_utils:serialize(Type, Payload),
#{
format_version => 1,
data => {bin, Bin}
}.
%% Unmashaling

View File

@ -1172,42 +1172,20 @@ try_attach_snapshot(Changes, AuxSt, _) ->
-define(TOP_VERSION, 6).
%@TODO MIGRATION: To thift binaries
%wrap_event_payload(Changes) ->
% marshal_event_payload(Changes, undefined).
wrap_event_payload(Changes) ->
marshal_event_payload(Changes, undefined).
%@TODO MIGRATION: To thift binaries
%wrap_event_payload_w_snapshot(Changes, St) ->
% StateSnapshot = encode_state(?CT_ERLANG_BINARY, St),
% marshal_event_payload(Changes, StateSnapshot).
wrap_event_payload_w_snapshot(Changes, St) ->
StateSnapshot = encode_state(?CT_ERLANG_BINARY, St),
marshal_event_payload(Changes, StateSnapshot).
wrap_event_payload(Event) ->
ContentType = ?CT_ERLANG_BINARY,
Meta = #{
<<"vsn">> => ?TOP_VERSION,
<<"ct">> => ContentType
},
Data = encode_event(ContentType, Event),
#{format_version => undefined, data => [Meta, Data]}.
wrap_event_payload_w_snapshot(Event, St) ->
ContentType = ?CT_ERLANG_BINARY,
Meta = #{
<<"vsn">> => ?TOP_VERSION,
<<"ct">> => ContentType,
<<"state_snapshot">> => encode_state(ContentType, St)
},
Data = encode_event(ContentType, Event),
#{format_version => undefined, data => [Meta, Data]}.
%@TODO MIGRATION: To thift binaries
%marshal_event_payload(?party_ev(Changes), StateSnapshot) ->
% Type = {struct, struct, {dmsl_payment_processing_thrift, 'PartyEventData'}},
% Bin = hg_proto_utils:serialize(Type, #payproc_PartyEventData{changes = Changes, state_snapshot = StateSnapshot}),
% #{
% format_version => 1,
% data => {bin, Bin}
% }.
marshal_event_payload(?party_ev(Changes), StateSnapshot) ->
Type = {struct, struct, {dmsl_payment_processing_thrift, 'PartyEventData'}},
Bin = hg_proto_utils:serialize(Type, #payproc_PartyEventData{changes = Changes, state_snapshot = StateSnapshot}),
#{
format_version => 1,
data => {bin, Bin}
}.
unwrap_events(History) ->
[unwrap_event(E) || E <- History].
@ -1270,9 +1248,6 @@ decode_state(?CT_ERLANG_BINARY, undefined) ->
decode_state(?CT_ERLANG_BINARY, {bin, EncodedSt}) ->
binary_to_term(EncodedSt).
encode_event(?CT_ERLANG_BINARY, Event) ->
{bin, term_to_binary(Event)}.
decode_event(?CT_ERLANG_BINARY, {bin, EncodedEvent}) ->
binary_to_term(EncodedEvent).

View File

@ -803,208 +803,12 @@ marshal_recurrent_paytool_params(Params) ->
-spec marshal_event_payload([rec_payment_tool_change()]) ->
hg_machine:event_payload().
marshal_event_payload(Changes) ->
#{format_version => undefined, data => [marshal(change, Change) || Change <- Changes]}.
%@TODO MIGRATION: To thift binaries
%Type = {struct, struct, {dmsl_payment_processing_thrift, 'RecurrentPaymentToolEventData'}},
%Bin = hg_proto_utils:serialize(Type, #payproc_RecurrentPaymentToolEventData{changes = Changes}),
%#{
% format_version => 1,
% data => {bin, Bin}
%}.
%%
marshal(change, ?recurrent_payment_tool_has_created(RecPaymentTool)) ->
[1, #{
<<"change">> => <<"created">>,
<<"rec_payment_tool">> => marshal(rec_payment_tool, RecPaymentTool)
}];
marshal(change, ?recurrent_payment_tool_risk_score_changed(RiskScore)) ->
[1, #{
<<"change">> => <<"risk_score_changed">>,
<<"risk_score">> => marshal(risk_score, RiskScore)
}];
marshal(change, ?recurrent_payment_tool_route_changed(Route)) ->
[1, #{
<<"change">> => <<"route_changed">>,
<<"route">> => hg_routing:marshal(Route)
}];
marshal(change, ?recurrent_payment_tool_has_acquired(Token)) ->
[1, #{
<<"change">> => <<"acquired">>,
<<"token">> => marshal(str, Token)
}];
marshal(change, ?recurrent_payment_tool_has_abandoned()) ->
[1, #{
<<"change">> => <<"abandoned">>
}];
marshal(change, ?recurrent_payment_tool_has_failed(Failure)) ->
[1, #{
<<"change">> => <<"failed">>,
<<"failure">> => marshal(failure, Failure)
}];
marshal(change, ?session_ev(Payload)) ->
[1, #{
<<"change">> => <<"session_change">>,
<<"payload">> => marshal(session_change, Payload)
}];
%%
marshal(rec_payment_tool, #payproc_RecurrentPaymentTool{} = RecPaymentTool) ->
Type = {struct, struct, {dmsl_payment_processing_thrift, 'RecurrentPaymentToolEventData'}},
Bin = hg_proto_utils:serialize(Type, #payproc_RecurrentPaymentToolEventData{changes = Changes}),
#{
<<"id">> => marshal(str, RecPaymentTool#payproc_RecurrentPaymentTool.id),
<<"shop_id">> => marshal(str, RecPaymentTool#payproc_RecurrentPaymentTool.shop_id),
<<"party_id">> => marshal(str, RecPaymentTool#payproc_RecurrentPaymentTool.party_id),
<<"party_revision">> => marshal(int, RecPaymentTool#payproc_RecurrentPaymentTool.party_revision),
<<"domain_revision">> => marshal(int, RecPaymentTool#payproc_RecurrentPaymentTool.domain_revision),
<<"status">> => marshal(status, RecPaymentTool#payproc_RecurrentPaymentTool.status),
<<"created_at">> => marshal(str, RecPaymentTool#payproc_RecurrentPaymentTool.created_at),
<<"payment_resource">> => marshal(
disposable_payment_resource,
RecPaymentTool#payproc_RecurrentPaymentTool.payment_resource
),
<<"rec_token">> => marshal(str, RecPaymentTool#payproc_RecurrentPaymentTool.rec_token),
<<"route">> => hg_routing:marshal(RecPaymentTool#payproc_RecurrentPaymentTool.route),
<<"minimal_payment_cost">> => marshal(cash, RecPaymentTool#payproc_RecurrentPaymentTool.minimal_payment_cost)
};
marshal(risk_score, low) ->
<<"low">>;
marshal(risk_score, high) ->
<<"high">>;
marshal(risk_score, fatal) ->
<<"fatal">>;
marshal(sub_failure, undefined) ->
undefined;
marshal(sub_failure, #domain_SubFailure{} = SubFailure) ->
genlib_map:compact(#{
<<"code">> => marshal(str , SubFailure#domain_SubFailure.code),
<<"sub" >> => marshal(sub_failure, SubFailure#domain_SubFailure.sub )
});
marshal(failure, {operation_timeout, _}) ->
[1, <<"operation_timeout">>];
marshal(failure, {failure, #domain_Failure{} = Failure}) ->
[1, [<<"failure">>, genlib_map:compact(#{
<<"code" >> => marshal(str , Failure#domain_Failure.code ),
<<"reason">> => marshal(str , Failure#domain_Failure.reason),
<<"sub" >> => marshal(sub_failure, Failure#domain_Failure.sub )
})]];
%% Session change
marshal(session_change, ?session_started()) ->
<<"started">>;
marshal(session_change, ?session_finished(Result)) ->
[
<<"finished">>,
marshal(session_status, Result)
];
marshal(session_change, ?session_suspended(Tag, TimeoutBehaviour)) ->
[
<<"suspended">>,
marshal(str, Tag),
marshal(timeout_behaviour, TimeoutBehaviour)
];
marshal(session_change, ?session_activated()) ->
<<"activated">>;
marshal(session_change, ?trx_bound(Trx)) ->
[
<<"transaction_bound">>,
marshal(trx, Trx)
];
marshal(session_change, ?proxy_st_changed(ProxySt)) ->
[
<<"proxy_state_changed">>,
marshal(bin, {bin, ProxySt})
];
marshal(session_change, ?interaction_requested(UserInteraction)) ->
[
<<"interaction_requested">>,
marshal(interaction, UserInteraction)
];
marshal(session_status, ?session_succeeded()) ->
<<"succeeded">>;
marshal(session_status, ?session_failed(PayloadFailure)) ->
[
<<"failed">>,
marshal(failure, PayloadFailure)
];
%%
marshal(trx, #domain_TransactionInfo{} = TransactionInfo) ->
genlib_map:compact(#{
<<"id">> => marshal(str, TransactionInfo#domain_TransactionInfo.id),
<<"timestamp">> => marshal(str, TransactionInfo#domain_TransactionInfo.timestamp),
<<"extra">> => marshal(map_str, TransactionInfo#domain_TransactionInfo.extra)
});
marshal(interaction, {redirect, {get_request, #'BrowserGetRequest'{uri = URI}}}) ->
#{<<"redirect">> =>
[
<<"get_request">>,
marshal(str, URI)
]
};
marshal(interaction, {redirect, {post_request, #'BrowserPostRequest'{uri = URI, form = Form}}}) ->
#{<<"redirect">> =>
[
<<"post_request">>,
#{
<<"uri">> => marshal(str, URI),
<<"form">> => marshal(map_str, Form)
}
]
};
marshal(timeout_behaviour, {callback, Callback}) ->
#{<<"callback">> => marshal(str, Callback)};
marshal(timeout_behaviour, {operation_failure, Failure}) ->
#{<<"operation_failure">> => marshal(failure, Failure)};
%%
marshal(status, ?recurrent_payment_tool_created()) ->
<<"created">>;
marshal(status, ?recurrent_payment_tool_acquired()) ->
<<"acquired">>;
marshal(status, ?recurrent_payment_tool_abandoned()) ->
<<"abandoned">>;
marshal(status, ?recurrent_payment_tool_failed(Failure)) ->
[
<<"failed">>,
marshal(failure, Failure)
];
marshal(disposable_payment_resource, #domain_DisposablePaymentResource{} = PaymentResource) ->
#{
<<"payment_tool">> => hg_payment_tool:marshal(PaymentResource#domain_DisposablePaymentResource.payment_tool),
<<"payment_session_id">> => marshal(str, PaymentResource#domain_DisposablePaymentResource.payment_session_id),
<<"client_info">> => marshal(client_info, PaymentResource#domain_DisposablePaymentResource.client_info)
};
marshal(cash, #domain_Cash{amount = Amount, currency = CurrencyRef}) ->
#{
<<"amount">> => marshal(int, Amount),
<<"currency_ref">> => marshal(str, CurrencyRef#domain_CurrencyRef.symbolic_code)
};
%%
marshal(client_info, #domain_ClientInfo{} = ClientInfo) ->
genlib_map:compact(#{
<<"ip_address">> => marshal(str, ClientInfo#domain_ClientInfo.ip_address),
<<"fingerprint">> => marshal(str, ClientInfo#domain_ClientInfo.fingerprint)
});
%%
marshal(_, Other) ->
Other.
format_version => 1,
data => {bin, Bin}
}.
%%
%% Unmarshalling