@@ -63,43 +63,48 @@ internal class RelExclude(
63
63
structValue : StructValue <* >,
64
64
exclusions : ExcludeNode
65
65
): PartiQLValue {
66
- val leavesSteps = exclusions.leaves.map { leaf -> leaf.step }
67
- val branches = exclusions.branches
68
- if (leavesSteps.any { it is ExcludeStep .StructWildcard }) {
69
- // tuple wildcard at current level. return empty struct
70
- return structValue<PartiQLValue >()
71
- }
72
- val attrsToRemove = leavesSteps.filterIsInstance<ExcludeStep .StructField >()
73
- .map { it.attr }
74
- .toSet()
75
- val entriesWithRemoved = structValue.entries.filter { structField ->
76
- ! attrsToRemove.contains(structField.first)
77
- }
78
- val finalStruct = entriesWithRemoved.map { structField ->
79
- val name = structField.first
80
- var expr = structField.second
81
- // apply case-sensitive tuple attr exclusions
82
- val structFieldCaseSensitiveKey = ExcludeStep .StructField (name, ExcludeFieldCase .SENSITIVE )
83
- branches.find {
84
- it.step == structFieldCaseSensitiveKey
85
- }?.let {
86
- expr = excludeOnPartiQLValue(expr, it)
87
- }
88
- // apply case-insensitive tuple attr exclusions
89
- val structFieldCaseInsensitiveKey = ExcludeStep .StructField (name, ExcludeFieldCase .INSENSITIVE )
90
- branches.find {
91
- it.step == structFieldCaseInsensitiveKey
92
- }?.let {
93
- expr = excludeOnPartiQLValue(expr, it)
66
+ val attrsToRemove = exclusions.leaves.mapNotNull { leaf ->
67
+ when (val leafStep = leaf.step) {
68
+ is ExcludeStep .StructWildcard -> {
69
+ // tuple wildcard at current level. return empty struct
70
+ return structValue<PartiQLValue >()
71
+ }
72
+ is ExcludeStep .StructField -> leafStep.attr
73
+ is ExcludeStep .CollIndex , is ExcludeStep .CollWildcard -> null
94
74
}
95
- // apply tuple wildcard exclusions
96
- val tupleWildcardKey = ExcludeStep .StructWildcard
97
- branches.find {
98
- it.step == tupleWildcardKey
99
- }?.let {
100
- expr = excludeOnPartiQLValue(expr, it)
75
+ }.toSet()
76
+ val branches = exclusions.branches
77
+ val finalStruct = structValue.entries.mapNotNull { structField ->
78
+ if (attrsToRemove.contains(structField.first)) {
79
+ // struct attr is to be removed at current level
80
+ null
81
+ } else {
82
+ // deeper level exclusions
83
+ val name = structField.first
84
+ var expr = structField.second
85
+ // apply case-sensitive tuple attr exclusions at deeper levels
86
+ val structFieldCaseSensitiveKey = ExcludeStep .StructField (name, ExcludeFieldCase .SENSITIVE )
87
+ branches.find {
88
+ it.step == structFieldCaseSensitiveKey
89
+ }?.let {
90
+ expr = excludeOnPartiQLValue(expr, it)
91
+ }
92
+ // apply case-insensitive tuple attr exclusions at deeper levels
93
+ val structFieldCaseInsensitiveKey = ExcludeStep .StructField (name, ExcludeFieldCase .INSENSITIVE )
94
+ branches.find {
95
+ it.step == structFieldCaseInsensitiveKey
96
+ }?.let {
97
+ expr = excludeOnPartiQLValue(expr, it)
98
+ }
99
+ // apply tuple wildcard exclusions at deeper levels
100
+ val tupleWildcardKey = ExcludeStep .StructWildcard
101
+ branches.find {
102
+ it.step == tupleWildcardKey
103
+ }?.let {
104
+ expr = excludeOnPartiQLValue(expr, it)
105
+ }
106
+ Pair (name, expr)
101
107
}
102
- Pair (name, expr)
103
108
}
104
109
return structValue(finalStruct)
105
110
}
@@ -124,33 +129,34 @@ internal class RelExclude(
124
129
type : PartiQLValueType ,
125
130
exclusions : ExcludeNode
126
131
): PartiQLValue {
127
- val leavesSteps = exclusions.leaves.map { leaf -> leaf.step }
128
- val branches = exclusions.branches
129
- if (leavesSteps.any { it is ExcludeStep .CollWildcard }) {
130
- // collection wildcard at current level. return empty collection
131
- return newCollValue(type, emptyList())
132
- } else {
133
- val indexesToRemove = leavesSteps.filterIsInstance<ExcludeStep .CollIndex >()
134
- .map { it.index }
135
- .toSet()
136
- val collWithRemoved = when (coll) {
137
- is BagValue -> coll
138
- is ListValue , is SexpValue -> coll.filterIndexed { index, _ ->
139
- ! indexesToRemove.contains(index)
132
+ val indexesToRemove = exclusions.leaves.mapNotNull { leaf ->
133
+ when (val leafStep = leaf.step) {
134
+ is ExcludeStep .CollWildcard -> {
135
+ // collection wildcard at current level. return empty collection
136
+ return newCollValue(type, emptyList())
140
137
}
138
+ is ExcludeStep .CollIndex -> leafStep.index
139
+ is ExcludeStep .StructField , is ExcludeStep .StructWildcard -> null
141
140
}
142
- val finalColl = collWithRemoved.mapIndexed { index, element ->
141
+ }.toSet()
142
+ val branches = exclusions.branches
143
+ val finalColl = coll.mapIndexedNotNull { index, element ->
144
+ if (indexesToRemove.contains(index)) {
145
+ // coll index is to be removed at current level
146
+ null
147
+ } else {
148
+ // deeper level exclusions
143
149
var expr = element
144
150
if (coll is ListValue || coll is SexpValue ) {
145
- // apply collection index exclusions for lists and sexps
151
+ // apply collection index exclusions at deeper levels for lists and sexps
146
152
val elementKey = ExcludeStep .CollIndex (index)
147
153
branches.find {
148
154
it.step == elementKey
149
155
}?.let {
150
156
expr = excludeOnPartiQLValue(element, it)
151
157
}
152
158
}
153
- // apply collection wildcard exclusions for lists, bags, and sexps
159
+ // apply collection wildcard exclusions at deeper levels for lists, bags, and sexps
154
160
val collectionWildcardKey = ExcludeStep .CollWildcard
155
161
branches.find {
156
162
it.step == collectionWildcardKey
@@ -159,8 +165,8 @@ internal class RelExclude(
159
165
}
160
166
expr
161
167
}
162
- return newCollValue(type, finalColl)
163
168
}
169
+ return newCollValue(type, finalColl)
164
170
}
165
171
166
172
@OptIn(PartiQLValueExperimental ::class )
0 commit comments