@@ -777,6 +777,13 @@ Result<SchemaProjection> Project(const Schema& expected_schema,
777777
778778namespace {
779779
780+ void CopyCustomAttributes (const ::avro::CustomAttributes& source,
781+ ::avro::CustomAttributes& target) {
782+ for (const auto & attr_pair : source.attributes ()) {
783+ target.addAttribute (attr_pair.first , attr_pair.second , /* addQuote=*/ false );
784+ }
785+ }
786+
780787Result<::avro::NodePtr> CreateRecordNodeWithFieldIds (const ::avro::NodePtr& original_node,
781788 const MappedField& field) {
782789 auto new_record_node = std::make_shared<::avro::NodeRecord>();
@@ -857,14 +864,7 @@ Result<::avro::NodePtr> CreateArrayNodeWithFieldIds(const ::avro::NodePtr& origi
857864 if (original_node->customAttributes () > 0 ) {
858865 ::avro::CustomAttributes merged_attributes;
859866 const auto & original_attrs = original_node->customAttributesAt (0 );
860- const auto & existing_attrs = original_attrs.attributes ();
861- for (const auto & attr_pair : existing_attrs) {
862- // Skip element-id as we might set it differently
863- if (attr_pair.first != kElementIdProp ) {
864- merged_attributes.addAttribute (attr_pair.first , attr_pair.second ,
865- /* addQuote=*/ false );
866- }
867- }
867+ CopyCustomAttributes (original_attrs, merged_attributes);
868868 // Add merged attributes if we found any
869869 if (merged_attributes.attributes ().size () > 0 ) {
870870 new_array_node->addCustomAttributesForField (merged_attributes);
@@ -898,17 +898,10 @@ Result<::avro::NodePtr> CreateArrayNodeWithFieldIds(const ::avro::NodePtr& origi
898898 std::to_string (*element_field.field_id ),
899899 /* addQuote=*/ false );
900900
901- // Then merge any custom attributes from original node (except element-id)
901+ // Then merge any custom attributes from original node
902902 if (original_node->customAttributes () > 0 ) {
903903 const auto & original_attrs = original_node->customAttributesAt (0 );
904- const auto & existing_attrs = original_attrs.attributes ();
905- for (const auto & attr_pair : existing_attrs) {
906- // Skip element-id as we've already set it above
907- if (attr_pair.first != kElementIdProp ) {
908- merged_attributes.addAttribute (attr_pair.first , attr_pair.second ,
909- /* addQuote=*/ false );
910- }
911- }
904+ CopyCustomAttributes (original_attrs, merged_attributes);
912905 }
913906
914907 // Add all attributes at once
@@ -969,26 +962,12 @@ Result<::avro::NodePtr> CreateMapNodeWithFieldIds(const ::avro::NodePtr& origina
969962 if (original_node->customAttributes () > 0 ) {
970963 // Merge attributes for key (index 0)
971964 const auto & original_key_attrs = original_node->customAttributesAt (0 );
972- const auto & existing_key_attrs = original_key_attrs.attributes ();
973- for (const auto & attr_pair : existing_key_attrs) {
974- // Skip if it's the key ID property we're already setting
975- if (attr_pair.first != kKeyIdProp ) {
976- key_attributes.addAttribute (attr_pair.first , attr_pair.second ,
977- /* addQuote=*/ false );
978- }
979- }
965+ CopyCustomAttributes (original_key_attrs, key_attributes);
980966
981967 // Merge attributes for value (index 1)
982968 if (original_node->customAttributes () > 1 ) {
983969 const auto & original_value_attrs = original_node->customAttributesAt (1 );
984- const auto & existing_value_attrs = original_value_attrs.attributes ();
985- for (const auto & attr_pair : existing_value_attrs) {
986- // Skip if it's the value ID property we're already setting
987- if (attr_pair.first != kValueIdProp ) {
988- value_attributes.addAttribute (attr_pair.first , attr_pair.second ,
989- /* addQuote=*/ false );
990- }
991- }
970+ CopyCustomAttributes (original_value_attrs, value_attributes);
992971 }
993972 }
994973
0 commit comments