oatpp-postgresql icon indicating copy to clipboard operation
oatpp-postgresql copied to clipboard

API question: other types

Open dsmyth opened this issue 5 years ago • 3 comments

In file: oatpp-postgresql/Types.hpp

Question

What is your roadmap for supporting other types? I'm particularly interested in json, jsonb and arrays (of floats).

Thanks -- Don

dsmyth avatar Dec 30 '20 01:12 dsmyth

Hello @dsmyth ,

Just updated the readme with the list of supported data-types. :)

What is your roadmap for supporting other types? I'm particularly interested in json, jsonb and arrays (of floats).

At the moment there is no roadmap for this. A better types-support will be developed dynamically based on community requests/PRs/and ease of implementation.

My estimations:

  • Arrays - easy - might be added pretty soon (others also asked for array support).
  • JSON - hard - investigation needed.
  • JSONB - hard - investigation needed.

Also, if you'd like to contribute - I'll provide the necessary assistance.

Best Regards, Leonid

lganzzzo avatar Dec 30 '20 02:12 lganzzzo

I'm willing to try adding arrays -- my current need is a float4 array. If I catch it in the deserializer, I can see my data in memory: In pgsql:

twdb=# select boxbounds from captures where captureid=1;
        boxbounds        
-------------------------
 {0,1.1,2.2,3.3,4.4,5.5}

In gdb:

(gdb) x/68xb data.data
0x7f45c40f8740:	0x00	0x00	0x00	0x01	0x00	0x00	0x00	0x00
0x7f45c40f8748:	0x00	0x00	0x02	0xbc	0x00	0x00	0x00	0x06
0x7f45c40f8750:	0x00	0x00	0x00	0x01	0x00	0x00	0x00	0x04
0x7f45c40f8758:	0x00	0x00	0x00	0x00	0x00	0x00	0x00	0x04
0x7f45c40f8760:	0x3f	0x8c	0xcc	0xcd	0x00	0x00	0x00	0x04
0x7f45c40f8768:	0x40	0x0c	0xcc	0xcd	0x00	0x00	0x00	0x04
0x7f45c40f8770:	0x40	0x53	0x33	0x33	0x00	0x00	0x00	0x04
0x7f45c40f8778:	0x40	0x8c	0xcc	0xcd	0x00	0x00	0x00	0x04
0x7f45c40f8780:	0x40	0xb0	0x00	0x00

Offset 58, 68, 70, 78 and 80 are the IEEE754 representation of the 4 byte floating point numbers in the field. Can you point me to how to parse this data structure?

Here's my start at adding the deserializer method:

oatpp::Void Deserializer::deserializeArray(const Deserializer* _this, const InData& data, const Type* type) {

    (void) _this;
    (void) type;

    switch(data.oid) {
        case FLOAT4ARRAYOID: return oatpp::Vector<Float32>();
        case FLOAT8ARRAYOID: return oatpp::Vector<Float64>();
    }

    if(data.isNull) {
        return oatpp::postgresql::Uuid();
    }

    return postgresql::Uuid((p_char8)data.data);
}

dsmyth avatar Jan 01 '21 00:01 dsmyth

Hey @dsmyth ,

Please create a draft PR so that we can collaborate on this feature.

Can you point me to how to parse this data structure?

First, let's start by creating a template method for parsing arrays to oatpp::Vector, oatpp::List, oatpp::UnorderedSet. We ganna have a template method because functionality will be mostly the same:

Let's use JSON deserializer as a reference - See JSON array deserialization

template<class Collection> 
oatpp::Void Deserializer::deserializeArray(const Deserializer* _this, const InData& data, const Type* type) {

  auto polymorphicDispatcher = static_cast<const typename Collection::Class::PolymorphicDispatcher*>(type->polymorphicDispatcher);
  
  auto itemType = *type->params.begin(); // Get "wanted" type of the list item

  // TODO - check that wanted itemType is compatible with the data OID item type.

  if(data.isNull) {
    // return nullptr with the type. - after we've checked the type.
    return oatpp::Void(nullptr, type);
  }

  auto listWrapper = polymorphicDispatcher->createObject(); // Instantiate list of the "wanted" type
  const auto& list = listWrapper.template staticCast<Collection>();

  // TODO - read "InData data" and push items to list

  return list;

}

Then, reference this method by referencing it here - https://github.com/oatpp/oatpp-postgresql/blob/master/src/oatpp-postgresql/mapping/Deserializer.cpp#L72 - for Vector, List, and UnorderedSet

lganzzzo avatar Jan 01 '21 04:01 lganzzzo