is it possible to have a partial fallback on struct custom types?

Đang mở
#1,244 2 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
5/5
Thời gian dự kiến
Hơn một tuần
Mức phù hợp với người mới
25/100
Loại issue
Tính năng
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Đình trệ
Công nghệ
cpp, yaml
Lĩnh vực
backend

Hướng nghiên cứu

Start by reading the linked conversion tutorial and the Node::as and convert<T> APIs. The issue asks whether structured custom types can support per-field fallback values, including an override passed to as; it does not identify files or tests. Done means deciding whether this API belongs in yaml-cpp and, if so, specifying and testing how partial fallbacks work.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

for e.g. given
https://github.com/jbeder/yaml-cpp/wiki/Tutorial#converting-tofrom-native-data-types

namespace YAML {
template<>
struct convert<Vec3> {
  static Node encode(const Vec3& rhs) {
    Node node;
    node.push_back(rhs.x);
    node.push_back(rhs.y);
    node.push_back(rhs.z);
    return node;
  }

  static bool decode(const Node& node, Vec3& rhs) {
    //if(node.size() != 3) {
    //  return false;
   // }

    rhs.x = node["x"].as<double>();
    rhs.y = node["y"].as<double>();
    rhs.z = node["z"].as<double>();
    return true;
  }
};
}

would it be possible to have z as optional so the last decode line would be something like:

rhs.x = node["x"].as<double>();
rhs.y = node["y"].as<double>();
rhs.z = node["z"].as<double>(0.0); // note the fallback here

in this case instead of hard-coding a 0.0 would it be possible to pass a value as a fallback?

rhs.x = node["x"].as<double>();
rhs.y = node["y"].as<double>();
rhs.z = node["z"].as<double>(z_fallback);

and when using it to decode a YAML document that for e.g. has only x and y. is it possible to specify the Z fallback, kind of:

{
// without any fallback as x,y,z are present
YAML::Node node = YAML::Load("start: {x:1, y:3, z:0}");
Vec3 v = node["start"].as<Vec3>(); 
}
{
// z fallback
YAML::Node node = YAML::Load("start: {x:1, y:3}");
Vec3 v = node["start"].as<Vec3>({.z=z_fallback});  // here there is no way to pass a "partial fall back" as i am aware of.
}

a workaround for this partial fallback could be:
when decoding, use a special value to indicate fallback has happened, so after conversion with .as, check that special value and replace with the right fallback. But this requires a value to not be used and be reserved. not always possible.

another one is to use a global value for the fallback to change as needed before the .as call.


Is there any undocumented way to achieve such a partial fallback "elegantly"?
Would it make any sense from a yaml-cpp perspective to allow decoding having partial fall-back on a structured type?

Would it be possible to add in the convert template such kind of partial fallbacks functionalities?
like storing one T to be used as fallback, that can be overridden when calling the higher level .as with fallback that is passing it to template struct convert so it will use those as fallbacks? or something like that?

Ngôn ngữ chính
C++
Star
6.1k
Fork
2.2k
Merge trung bình
22 giờ 2 phút
Pull request đã merge (30 ngày)
1

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của jbeder/yaml-cpp

Tất cả issue của jbeder/yaml-cpp

Issue tương tự

Thêm issue về C++

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.