Skip to content

Commit d81aef9

Browse files
authored
Make sure builtin member types can also be renamed for ROOT (#799)
* Add a failing test for a datatype member rename Fix generation script enough to make things compile again * Add clarifying comments * Make sure also handle renamed members that are not components
1 parent dbad20d commit d81aef9

File tree

6 files changed

+71
-20
lines changed

6 files changed

+71
-20
lines changed

python/podio_gen/cpp_generator.py

Lines changed: 44 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -388,23 +388,42 @@ def _preprocess_schema_evolution_datatype(self, name, datatype):
388388
"""
389389
schema_evolution_datatype = deepcopy(datatype)
390390
needs_schema_evolution = False
391+
# We have to figure out whether we need to do schema evolution and how
392+
# we do it. We have to do it if either a (non-component) member is
393+
# affected or if a member that is a component type has a renamed member.
394+
# The things we need to do differ slightly in both cases
391395
for member in schema_evolution_datatype["Members"]:
392396
member_type = member.array_type if member.is_array else member.full_type
393-
if member_type in self.root_schema_dict:
394-
needs_schema_evolution = True
395-
replace_component_in_paths(
396-
member_type,
397-
_versioned(member_type, self.old_schema_version),
398-
schema_evolution_datatype["includes_data"],
399-
)
400-
if member.is_array:
401-
member.full_type = member.full_type.replace(
402-
member.array_type, _versioned(member.array_type, self.old_schema_version)
397+
for type_name, evolutions in self.root_schema_dict.items():
398+
if type_name == member_type:
399+
needs_schema_evolution = True
400+
# We have a component with a member that got renamed. Hence we
401+
# need to make sure we generate a version of the Data class with
402+
# the old component
403+
replace_component_in_paths(
404+
member_type,
405+
_versioned(member_type, self.old_schema_version),
406+
schema_evolution_datatype["includes_data"],
403407
)
404-
member.array_type = _versioned(member.array_type, self.old_schema_version)
405-
else:
406-
member.full_type = _versioned(member.full_type, self.old_schema_version)
407-
member.bare_type = _versioned(member.bare_type, self.old_schema_version)
408+
if member.is_array:
409+
member.full_type = member.full_type.replace(
410+
member.array_type,
411+
_versioned(member.array_type, self.old_schema_version),
412+
)
413+
member.array_type = _versioned(member.array_type, self.old_schema_version)
414+
else:
415+
member.full_type = _versioned(member.full_type, self.old_schema_version)
416+
member.bare_type = _versioned(member.bare_type, self.old_schema_version)
417+
for evolution in evolutions:
418+
if (
419+
isinstance(evolution, RenamedMember)
420+
and member.name == evolution.member_name_new
421+
):
422+
# We have a member that has been renamed. We just need to
423+
# make sure we get a version of the Data class with the old
424+
# name
425+
needs_schema_evolution = True
426+
member.name = evolution.member_name_old
408427

409428
if needs_schema_evolution:
410429
print(f" Preparing explicit schema evolution for {name}")
@@ -459,7 +478,11 @@ def _prepare_iorules(self):
459478
for schema_change in schema_changes:
460479
if isinstance(schema_change, RenamedMember):
461480
# find out the type of the renamed member
462-
component = self.datamodel.components[type_name]
481+
component = self.datamodel.components.get(type_name)
482+
is_datatype = False
483+
if component is None:
484+
is_datatype = True
485+
component = self.datamodel.datatypes[type_name]
463486
member_type = None
464487
for member in component["Members"]:
465488
if member.name == schema_change.member_name_new:
@@ -473,6 +496,10 @@ def _prepare_iorules(self):
473496
iorule = RootIoRule()
474497
iorule.sourceClass = type_name
475498
iorule.targetClass = type_name
499+
if is_datatype:
500+
iorule.sourceClass = f"{type_name}Data"
501+
iorule.targetClass = f"{type_name}Data"
502+
476503
iorule.version = self.old_schema_version
477504
iorule.source = f"{member_type} {schema_change.member_name_old}"
478505
iorule.target = schema_change.member_name_new
@@ -601,10 +628,8 @@ def _create_selection_xml(self):
601628
"version": self.datamodel.schema_version,
602629
"components": [DataType(c) for c in self.datamodel.components],
603630
"datatypes": [DataType(d) for d in self.datamodel.datatypes],
604-
"old_schema_components": [
605-
DataType(d)
606-
for d in self.root_schema_datatype_names | self.root_schema_component_names
607-
],
631+
"old_schema_components": [DataType(d) for d in self.root_schema_component_names],
632+
"old_schema_datatypes": [DataType(d) for d in self.root_schema_datatype_names],
608633
"iorules": self.root_schema_iorules,
609634
}
610635

python/templates/selection.xml.jinja2

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@
3838
<!-- previous schema components and pods -->
3939
{% for class in old_schema_components %}
4040
{{ class_selection(class) }}
41+
{% endfor %}
42+
{% for class in old_schema_datatypes %}
43+
{{ class_selection(class, postfix="Data") }}
4144
{% endfor %}
4245

4346
</selection>

tests/schema_evolution/datalayout_new.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ datatypes :
9191
Description : "Cluster"
9292
Author : "B. Hegner"
9393
Members:
94-
- double energy // cluster energy
94+
- double newEnergyName // cluster energy (renamed for testing purposes)
9595
OneToManyRelations:
9696
- ExampleHit Hits // hits contained in the cluster
9797
- ExampleCluster Clusters // sub clusters used to create this cluster

tests/schema_evolution/read_new_data.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#ifndef PODIO_TESTS_SCHEMAEVOLUTION_READNEWDATA_H // NOLINT(llvm-header-guard): folder structure not suitable
22
#define PODIO_TESTS_SCHEMAEVOLUTION_READNEWDATA_H // NOLINT(llvm-header-guard): folder structure not suitable
33

4+
#include "datamodel/ExampleClusterCollection.h"
45
#include "datamodel/ExampleHitCollection.h"
56
#include "datamodel/ExampleWithARelationCollection.h"
67
#include "datamodel/ExampleWithNamespaceCollection.h"
@@ -30,6 +31,14 @@ int readExampleHit(const podio::Frame& event) {
3031
return 0;
3132
}
3233

34+
int readExampleCluster(const podio::Frame& event) {
35+
const auto& coll = event.get<ExampleClusterCollection>("datatypeMemberRenameTest");
36+
const auto elem = coll[0];
37+
ASSERT_EQUAL(elem.newEnergyName(), 3.14, "Renamed datatype members does not have the expected value");
38+
39+
return 0;
40+
}
41+
3342
int readExampleWithNamespace(const podio::Frame& event) {
3443
const auto& coll = event.get<ex42::ExampleWithNamespaceCollection>("componentMemberRenameTest");
3544
auto elem = coll[0];
@@ -59,6 +68,7 @@ int read_new_data(const std::string& filename) {
5968

6069
int result = 0;
6170
result += readExampleHit(event);
71+
result += readExampleCluster(event);
6272
result += readExampleWithNamespace(event);
6373
result += readExampleWithARelation(event);
6474

tests/schema_evolution/schema_evolution.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,8 @@ evolutions:
1111

1212
EventInfo:
1313
class_renamed_to: EventInfoNewName
14+
15+
ExampleCluster:
16+
member_rename:
17+
- energy
18+
- newEnergyName

tests/schema_evolution/write_old_data.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#ifndef PODIO_TESTS_SCHEMAEVOLUTION_WRITEOLDDATA_H // NOLINT(llvm-header-guard): folder structure not suitable
22
#define PODIO_TESTS_SCHEMAEVOLUTION_WRITEOLDDATA_H // NOLINT(llvm-header-guard): folder structure not suitable
33

4+
#include "datamodel/ExampleClusterCollection.h"
45
#include "datamodel/ExampleHitCollection.h"
56
#include "datamodel/ExampleWithARelationCollection.h"
67
#include "datamodel/ExampleWithNamespaceCollection.h"
@@ -20,6 +21,12 @@ auto writeExampleHit() {
2021
return coll;
2122
}
2223

24+
auto writeExampleCluster() {
25+
ExampleClusterCollection coll;
26+
auto elem = coll.create(3.14);
27+
return coll;
28+
}
29+
2330
auto writeExampleWithNamespace() {
2431
ex42::ExampleWithNamespaceCollection coll;
2532
auto elem = coll.create();
@@ -40,6 +47,7 @@ auto writeExampleWithARelation() {
4047
podio::Frame createFrame() {
4148
podio::Frame event;
4249

50+
event.put(writeExampleCluster(), "datatypeMemberRenameTest");
4351
event.put(writeExampleHit(), "datatypeMemberAdditionTest");
4452
event.put(writeExampleWithNamespace(), "componentMemberRenameTest");
4553
event.put(writeExampleWithARelation(), "floatToDoubleMemberTest");

0 commit comments

Comments
 (0)