What the best ways to use decimals and datetimes with protocol buffers?

前端 未结 4 2024
后悔当初
后悔当初 2021-02-08 14:56

I would like to find out what is the optimum way of storing some common data type that were not included in the list supported by protocol buffers.

  • datetime (secon
4条回答
  •  不思量自难忘°
    2021-02-08 15:20

    Sorry, not a complete answer, but a "me too".

    I think this is a great question, one I'd love an answer to myself. The inability to natively describe fundamental types like datetimes and (for financial applications) fixed point decimals, or map them to language-specified or user-defined types is a real killer for me. Its more or less prevented me from being able to use the library, which I otherwise think is fantastic.

    Declaring your own "DateTime" or "FixedPoint" message in the proto grammar isn't really a solution, because you'll still need to convert your platform's representation to/from the generated objects manually, which is error prone. Additionally, these nested messages get stored as pointers to heap-allocated objects in C++, which is wildly inefficient when the underlying type is basically just a 64-bit integer.

    Specifically, I'd want to be able to write something like this in my proto files:

    message Something {
       required fixed64 time = 1 [cpp_type="boost::posix_time::ptime"];
       required int64 price = 2 [cpp_type="fixed_point"];
       ...
     };
    

    And I would be required to provide whatever glue was necessary to convert these types to/from fixed64 and int64 so that the serialization would work. Maybe thru something like adobe::promote?

提交回复
热议问题