oatpp / oatpp-mongo

Oat++ native BSON + MongoDB driver implementation based on Oat++ object-mapping sub-framework.
https://oatpp.io/
Apache License 2.0
6 stars 4 forks source link

Is there any example with DTO object containing an array/vector field? #9

Closed ns-wxin closed 2 years ago

ns-wxin commented 2 years ago

Looks like I may be able to do

DTO_FIELD(List, value);

then I have a std::vector vec; but I when do value->push_back(vec[i]), I got some SEGV error.

wonder if there's any example of saving and retrieving an array/vector of objects to/from MongoDB using this framework.

Thanks.

ns-wxin commented 2 years ago

This page (https://oatpp.io/docs/components/dto/) is somewhat helpful and I got it to work.

class FpDto : public oatpp::DTO {

  DTO_INIT(FpDto, DTO)

...
  DTO_FIELD(String, filename);
  DTO_FIELD(List<Float32>, imageEmbedding);
  DTO_FIELD(String, region);
  // DTO_FIELD(String, imageEmbedding);
};

static void fillEmbedding(const oatpp::Object<FpDto> &fpDto, std::vector<float>& embedding) {
    if (!embedding.empty()) {
        auto embList = oatpp::List<oatpp::Float32>::createShared();
        for (float val : embedding)
            embList->push_back(val);
        fpDto->imageEmbedding = embList;
    }
}