MCPcopy Create free account
hub / github.com/Samsung/UTopia / addFieldToDescriptorEnumType

Method addFieldToDescriptorEnumType

lib/generation/ProtobufDescriptor.cpp:114–157  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

112}
113
114google::protobuf::FieldDescriptorProto *
115ProtobufDescriptor::addFieldToDescriptorEnumType(
116 google::protobuf::DescriptorProto &Desc, const std::string &FieldName,
117 const Type &T) {
118 assert(T.isEnumType() && "Unexpected Program State");
119 auto EnumName = T.getTypeName();
120 // TypeName might contain ':' due to namespace or nested.
121 // Replacing them as ':' is not valid character for identifier
122 std::replace(EnumName.begin(), EnumName.end(), ':', '_');
123 if (Enums.find(EnumName) == Enums.end()) {
124 const auto *EnumDef = T.getGlobalDef();
125 if (EnumDef == nullptr)
126 return nullptr;
127 auto *EnumDesc = ProtoFile.add_enum_type();
128 EnumDesc->set_name(EnumName);
129 // First value of protobuf enum should be 0.
130 // Set default name as UNKNOWN and overwrite it if target enum has 0 value.
131 // Enum value should be globally unique in protobuf.
132 // Adding name of enum as prefix to avoid collision.
133 auto *DefaultVal = EnumDesc->add_value();
134 DefaultVal->set_name(EnumName + "_UNKNOWN");
135 DefaultVal->set_number(0);
136 std::set<int64_t> EnumValues;
137 bool HaveAlias = false;
138 for (auto EnumVal : EnumDef->getElements()) {
139 auto ElemVal = EnumVal.getValue();
140 auto *ValDesc =
141 ElemVal == 0 && EnumDesc->value(0).name() == EnumName + "_UNKNOWN"
142 ? EnumDesc->mutable_value(0)
143 : EnumDesc->add_value();
144 ValDesc->set_name(EnumName + "_" + EnumVal.getName());
145 ValDesc->set_number(ElemVal);
146 auto Result = EnumValues.insert(ElemVal);
147 if (!Result.second)
148 HaveAlias = true;
149 }
150 if (HaveAlias)
151 EnumDesc->mutable_options()->set_allow_alias(true);
152 Enums.emplace(EnumName);
153 }
154 return addFieldToDescriptor(Desc, FieldName,
155 google::protobuf::FieldDescriptorProto::TYPE_ENUM,
156 EnumName);
157}
158
159google::protobuf::FieldDescriptorProto *
160ProtobufDescriptor::addFieldToDescriptorPointerType(

Callers

nothing calls this directly

Calls 9

isEnumTypeMethod · 0.80
getTypeNameMethod · 0.80
beginMethod · 0.80
endMethod · 0.80
getGlobalDefMethod · 0.80
nameMethod · 0.80
findMethod · 0.45
getValueMethod · 0.45
getNameMethod · 0.45

Tested by

no test coverage detected