Skip to content

Commit f2f1ced

Browse files
thomasameiselTommy Meisel
andauthored
Flatten Swift telemetry dictionary (#27)
* flatten inline * flatten swift telemetry dictionary --------- Co-authored-by: Tommy Meisel <thmeisel@microsoft.com>
1 parent 20005ad commit f2f1ced

File tree

2 files changed

+60
-63
lines changed

2 files changed

+60
-63
lines changed

compiler/cpp/src/thrift/generate/t_java_generator.cc

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5292,7 +5292,6 @@ void t_java_generator::generate_java_struct_tuple_writer(ofstream& out, t_struct
52925292
}
52935293

52945294
indent(out) << "oprot.writeBitSet(optionals, " << optional_count << ");" << endl;
5295-
int j = 0;
52965295
for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
52975296
if ((*f_iter)->get_req() == t_field::T_OPTIONAL
52985297
|| (*f_iter)->get_req() == t_field::T_OPT_IN_REQ_OUT) {
@@ -5301,7 +5300,6 @@ void t_java_generator::generate_java_struct_tuple_writer(ofstream& out, t_struct
53015300
generate_serialize_field(out, (*f_iter), "struct.", false);
53025301
indent_down();
53035302
indent(out) << "}" << endl;
5304-
j++;
53055303
}
53065304
}
53075305
}

compiler/cpp/src/thrift/generate/t_swift_generator.cc

Lines changed: 60 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,9 @@ class t_swift_generator : public t_oop_generator {
162162
bool contains_event_name(t_struct* tstruct);
163163
void generate_swift_struct_telemetry_object_extension(ofstream& out, t_struct* tstruct);
164164
void generate_swift_struct_telemetry_event_extension(ofstream& out, t_struct* tstruct);
165-
void telemetry_dictionary_value(ofstream& out, t_type* type, string property_name, string pii_kind);
165+
void telemetry_struct_value(ofstream& out, t_struct* tstruct);
166+
void telemetry_dictionary_value(ofstream& out, string name, t_type* type, string property_name, string pii_kind);
167+
void telemetry_base_type_value(ofstream& out, t_base_type::t_base tbase, string property_name, string pii_kind);
166168
void generate_swift_struct_thrift_extension(ofstream& out,
167169
t_struct* tstruct,
168170
bool is_result,
@@ -341,7 +343,6 @@ public enum TelemetryValue: Equatable {
341343
case bool(Bool, piiKind: OTPiiKind?)
342344
case int(Int, piiKind: OTPiiKind?)
343345
case double(Double, piiKind: OTPiiKind?)
344-
case dictionary(TelemetryDictionary)
345346
}
346347
347348
)objc";
@@ -605,12 +606,10 @@ void t_swift_generator::generate_swift_struct(ofstream& out,
605606
vector<t_field*>::const_iterator m_iter;
606607

607608
for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
608-
out << endl;
609609
out << declare_property(*m_iter, is_private) << endl;
610+
out << endl;
610611
}
611612

612-
out << endl;
613-
614613
// init
615614

616615
if (!exclude_empty_init_ || !struct_has_required_fields(tstruct)) {
@@ -695,8 +694,6 @@ void t_swift_generator::generate_swift_struct_init(ofstream& out,
695694
}
696695

697696
block_close(out);
698-
699-
out << endl;
700697
}
701698

702699
/**
@@ -864,19 +861,26 @@ void t_swift_generator::generate_swift_struct_telemetry_object_extension(ofstrea
864861
indent(out) << "extension " << tstruct->get_name() << " : TelemetryObject";
865862
block_open(out);
866863

867-
out << endl;
868-
869864
out << indent() << "public func telemetryDictionary() -> TelemetryDictionary";
870865
block_open(out);
871866

872-
out << endl;
873-
874867
if (contains_event_name(tstruct)) {
875868
out << indent() << "var telemetryData = baseProperties()" << endl;
876869
} else {
877870
out << indent() << "var telemetryData = TelemetryDictionary()" << endl;
878871
}
879872

873+
telemetry_struct_value(out, tstruct);
874+
875+
out << indent() << "return telemetryData" << endl;
876+
877+
block_close(out);
878+
block_close(out);
879+
880+
out << endl;
881+
}
882+
883+
void t_swift_generator::telemetry_struct_value(ofstream& out, t_struct* tstruct) {
880884
for (const auto& member : tstruct->get_members()) {
881885
bool optional = field_is_optional(member);
882886

@@ -890,73 +894,37 @@ void t_swift_generator::generate_swift_struct_telemetry_object_extension(ofstrea
890894
block_open(out);
891895
}
892896

893-
out << indent() << "telemetryData[\"" << member->get_name() << "\"] = ";
894-
895897
string pii_kind = "nil";
896898
std::map<string, string>::iterator it = member->annotations_.find("PIIKind");
897899
if (it != member->annotations_.end()) {
898900
pii_kind = it->second;
899901
}
900-
telemetry_dictionary_value(out, member->get_type(), struct_property_name(member), pii_kind);
901902

902-
out << endl;
903+
telemetry_dictionary_value(out, member->get_name(), member->get_type(), struct_property_name(member), pii_kind);
903904

904905
if (optional) {
905906
block_close(out);
906907
}
907908
}
908-
909-
out << indent() << "return telemetryData" << endl;
910-
911-
block_close(out);
912-
block_close(out);
913-
914-
out << endl;
915909
}
916910

917-
void t_swift_generator::telemetry_dictionary_value(ofstream& out, t_type* type, string property_name, string pii_kind) {
911+
void t_swift_generator::telemetry_dictionary_value(ofstream& out, string name, t_type* type, string property_name, string pii_kind) {
918912
type = get_true_type(type);
919913

920914
if (type->is_base_type()) {
921-
t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
922-
switch (tbase) {
923-
case t_base_type::TYPE_STRING:
924-
out << ".string(" << property_name << ", piiKind: " << pii_kind << ")";
925-
break;
915+
out << indent() << "telemetryData[\"" << name << "\"] = ";
926916

927-
case t_base_type::TYPE_BOOL:
928-
out << ".bool(" << property_name << ", piiKind: " << pii_kind << ")";
929-
break;
930-
931-
case t_base_type::TYPE_I8:
932-
case t_base_type::TYPE_I16:
933-
case t_base_type::TYPE_I32:
934-
case t_base_type::TYPE_I64:
935-
out << ".int(" << property_name << ", piiKind: " << pii_kind << ")";
936-
break;
937-
938-
case t_base_type::TYPE_DOUBLE:
939-
out << ".double(" << property_name << ", piiKind: " << pii_kind << ")";
940-
break;
941-
942-
default:
943-
throw "compiler error: invalid base type " + type->get_name();
944-
break;
945-
}
917+
t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
918+
telemetry_base_type_value(out, tbase, property_name, pii_kind);
919+
out << endl;
946920
} else if (type->is_map()) {
947921
t_map *tmap = (t_map*)type;
948922

949-
out << ".dictionary({";
950-
951-
indent_up();
952-
out << endl;
953-
954-
out << indent() << "var dictionary = TelemetryDictionary()" << endl;
955923
out << indent() << "for (key, value) in " << property_name;
956924

957925
block_open(out);
958926

959-
out << indent() << "dictionary[";
927+
out << indent() << "telemetryData[";
960928

961929
t_type* key_type = get_true_type(tmap->get_key_type());
962930
if (key_type->is_string()) {
@@ -970,26 +938,57 @@ void t_swift_generator::telemetry_dictionary_value(ofstream& out, t_type* type,
970938

971939
out << "] = ";
972940

973-
telemetry_dictionary_value(out, tmap->get_val_type(), "value", pii_kind);
941+
t_type* value_type = get_true_type(tmap->get_val_type());
942+
if (!value_type->is_base_type()) {
943+
throw "compiler error: unsupported value type for map " + value_type->get_name();
944+
}
945+
946+
t_base_type::t_base tbase = ((t_base_type*)value_type)->get_base();
947+
telemetry_base_type_value(out, tbase, "value", pii_kind);
974948

975949
out << endl;
976950

977951
block_close(out);
978-
979-
out << indent() << "return dictionary" << endl;
980-
981-
block_close(out, false);
982-
out << "())";
983952
} else if (type->is_enum()) {
953+
out << indent() << "telemetryData[\"" << name << "\"] = ";
984954
out << ".string(" << property_name << ".telemetryName(), piiKind: " << pii_kind << ")";
955+
out << endl;
985956
} else if (type->is_struct()) {
986-
out << ".dictionary(" << property_name << ".telemetryDictionary())";
957+
out << indent() << "telemetryData.merge(" << property_name << ".telemetryDictionary()) { _, child in child }";
958+
out << endl;
987959
}
988960
else {
989961
throw "compiler error: invalid type (" + type_name(type) + ") for property \"" + property_name + "\"";
990962
}
991963
}
992964

965+
void t_swift_generator::telemetry_base_type_value(ofstream& out, t_base_type::t_base tbase, string property_name, string pii_kind) {
966+
switch (tbase) {
967+
case t_base_type::TYPE_STRING:
968+
out << ".string(" << property_name << ", piiKind: " << pii_kind << ")";
969+
break;
970+
971+
case t_base_type::TYPE_BOOL:
972+
out << ".bool(" << property_name << ", piiKind: " << pii_kind << ")";
973+
break;
974+
975+
case t_base_type::TYPE_I8:
976+
case t_base_type::TYPE_I16:
977+
case t_base_type::TYPE_I32:
978+
case t_base_type::TYPE_I64:
979+
out << ".int(" << property_name << ", piiKind: " << pii_kind << ")";
980+
break;
981+
982+
case t_base_type::TYPE_DOUBLE:
983+
out << ".double(" << property_name << ", piiKind: " << pii_kind << ")";
984+
break;
985+
986+
default:
987+
throw "compiler error: invalid base type";
988+
break;
989+
}
990+
}
991+
993992
/**
994993
* Generate the TStruct protocol implementation.
995994
*

0 commit comments

Comments
 (0)