Skip to content

Commit dfe403f

Browse files
committed
Flatten nested union and intersection schemas
1 parent 6edbcb7 commit dfe403f

1 file changed

Lines changed: 21 additions & 2 deletions

File tree

src/main/java/org/glavo/nbt/validation/NBTSchema.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.glavo.nbt.tag.TagType;
2727
import org.jetbrains.annotations.Contract;
2828

29+
import java.util.ArrayList;
2930
import java.util.List;
3031
import java.util.Objects;
3132

@@ -79,7 +80,16 @@ static <T extends Tag> NBTSchema<T> union(NBTSchema<? extends T>... schemas) {
7980
if (schemas.length == 1) {
8081
return narrow(schemas[0]);
8182
}
82-
return new UnionSchema<>(List.of(schemas));
83+
84+
var list = new ArrayList<NBTSchema<? extends T>>(schemas.length);
85+
for (NBTSchema<? extends T> schema : schemas) {
86+
if (schema instanceof UnionSchema<? extends T> union) {
87+
list.addAll(union.schemas());
88+
} else {
89+
list.add(schema);
90+
}
91+
}
92+
return new UnionSchema<>(List.copyOf(list));
8393
}
8494

8595
/// Creates a schema that validates only if all the given schemas validate.
@@ -94,7 +104,16 @@ static <T extends Tag> NBTSchema<T> intersection(NBTSchema<? extends T>... schem
94104
if (schemas.length == 1) {
95105
return narrow(schemas[0]);
96106
}
97-
return new IntersectionSchema<>(List.of(schemas));
107+
108+
var list = new ArrayList<NBTSchema<? extends T>>(schemas.length);
109+
for (NBTSchema<? extends T> schema : schemas) {
110+
if (schema instanceof IntersectionSchema<? extends T> intersection) {
111+
list.addAll(intersection.schemas());
112+
} else {
113+
list.add(schema);
114+
}
115+
}
116+
return new IntersectionSchema<>(List.copyOf(list));
98117
}
99118

100119
/// Creates a schema that validates only if both of the given schemas validate.

0 commit comments

Comments
 (0)