Skip to content

Commit ad51db7

Browse files
lib/rb: Handle the PreviouslyKnownAs annotation seamlessly
1 parent b60279b commit ad51db7

File tree

4 files changed

+46
-8
lines changed

4 files changed

+46
-8
lines changed

compiler/cpp/src/generate/t_rb_generator.cc

+10-5
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,8 @@ class t_rb_generator : public t_oop_generator {
134134
t_type* field_type,
135135
const std::string& field_name,
136136
t_const_value* field_value,
137-
bool optional);
137+
bool optional,
138+
bool embed_annotations);
138139

139140
/**
140141
* Service-level generation functions
@@ -747,7 +748,8 @@ void t_rb_generator::generate_field_defns(t_rb_ofstream& out, t_struct* tstruct)
747748
(*f_iter)->get_type(),
748749
(*f_iter)->get_name(),
749750
(*f_iter)->get_value(),
750-
(*f_iter)->get_req() == t_field::T_OPTIONAL);
751+
(*f_iter)->get_req() == t_field::T_OPTIONAL,
752+
true);
751753
}
752754
out.indent_down();
753755
out << endl;
@@ -760,7 +762,8 @@ void t_rb_generator::generate_field_data(t_rb_ofstream& out,
760762
t_type* field_type,
761763
const std::string& field_name = "",
762764
t_const_value* field_value = NULL,
763-
bool optional = false) {
765+
bool optional = false,
766+
bool embed_annotations = false) {
764767
field_type = get_true_type(field_type);
765768

766769
// Begin this field's defn
@@ -804,8 +807,10 @@ void t_rb_generator::generate_field_data(t_rb_ofstream& out,
804807
out << ", enum_class: " << full_type_name(field_type);
805808
}
806809

807-
out << ", legacy_annotations: THRIFT_FIELD_" << upcase_string(field_name) << "_LEGACY_ANNOTATIONS";
808-
out << ", structured_annotations: THRIFT_FIELD_" << upcase_string(field_name) << "_STRUCTURED_ANNOTATIONS";
810+
if (embed_annotations) {
811+
out << ", legacy_annotations: THRIFT_FIELD_" << upcase_string(field_name) << "_LEGACY_ANNOTATIONS";
812+
out << ", structured_annotations: THRIFT_FIELD_" << upcase_string(field_name) << "_STRUCTURED_ANNOTATIONS";
813+
}
809814

810815
// End of this field's defn
811816
out << "}";

lib/rb/lib/thrift/definition.rb

+19
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
11
module Thrift
2+
class DefaultCanonicalNameExtractor
3+
class << self
4+
def extract(definition)
5+
[definition.struct_type]
6+
end
7+
end
8+
end
9+
210
class StructDefinition
311
attr_reader :klass
412

@@ -25,6 +33,12 @@ def name
2533
def struct_type
2634
"#{namespace}.#{name}"
2735
end
36+
37+
def canonical_names
38+
CANONICAL_NAME_EXTRACTORS.reduce([]) do |acc, cur|
39+
acc + cur.extract(self)
40+
end
41+
end
2842
end
2943

3044
class ServiceDefinition < StructDefinition
@@ -51,6 +65,7 @@ def service_type
5165

5266
STRUCT_DEFINITIONS = {}
5367
SERVICE_DEFINITIONS = {}
68+
CANONICAL_NAME_EXTRACTORS = [DefaultCanonicalNameExtractor]
5469

5570
class << self
5671
def register_struct_type(klass)
@@ -62,5 +77,9 @@ def register_service_type(klass)
6277
definition = ServiceDefinition.new(klass)
6378
SERVICE_DEFINITIONS[definition.service_type] = definition
6479
end
80+
81+
def register_canonical_name_extractor(klass)
82+
CANONICAL_NAME_EXTRACTORS << klass
83+
end
6584
end
6685
end

lib/rb/lib/thrift/types/annotation/naming_types.rb

+14
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/rb/lib/thrift/types/value_types.rb

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)