Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[struct_json, struct_xml, struct_yaml][feat]support alias #404

Merged
merged 1 commit into from
Aug 10, 2023

Conversation

qicosmos
Copy link
Collaborator

@qicosmos qicosmos commented Aug 10, 2023

Why

support reflect with alias.

What is changing

Example

struct next_obj_t {
  int x;
  int y;
};

// struct alias name and fileds alias names.
REFLECTION_ALIAS(next_obj_t, "next", FLDALIAS(&next_obj_t::x, "w"),
                 FLDALIAS(&next_obj_t::y, "h"));

struct out_object {
  std::unique_ptr<int> id;
  std::string_view name;
  next_obj_t obj;
  REFLECTION_ALIAS(out_object, "qi", FLDALIAS(&out_object::id, "i"),
                   FLDALIAS(&out_object::name, "na"),
                   FLDALIAS(&out_object::obj, "obj"));
};

void test_alias() {
  out_object m{std::make_unique<int>(20), "tom", {21, 42}};
  std::string xml_str;
  iguana::to_xml(m, xml_str);

  std::cout << xml_str << "\n";

  out_object m1;
  iguana::from_xml(m1, xml_str);
  assert(m1.name == "tom");
  assert(m1.obj.x == 21);
  assert(m1.obj.y == 42);
}

@qicosmos qicosmos merged commit d85658c into alibaba:main Aug 10, 2023
28 checks passed
@qicosmos qicosmos deleted the reflection_alias branch August 10, 2023 11:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant