Skip to content

Commit 3ef0b76

Browse files
authored
Simplify semanticEquals (#6388)
1 parent 8168fc0 commit 3ef0b76

File tree

1 file changed

+21
-209
lines changed
  • libraries/apollo-ast/src/commonMain/kotlin/com/apollographql/apollo/ast/internal

1 file changed

+21
-209
lines changed
Lines changed: 21 additions & 209 deletions
Original file line numberDiff line numberDiff line change
@@ -1,221 +1,33 @@
11
package com.apollographql.apollo.ast.internal
22

3-
import com.apollographql.apollo.ast.GQLArgument
4-
import com.apollographql.apollo.ast.GQLBooleanValue
5-
import com.apollographql.apollo.ast.GQLDirective
6-
import com.apollographql.apollo.ast.GQLDirectiveDefinition
7-
import com.apollographql.apollo.ast.GQLEnumTypeDefinition
8-
import com.apollographql.apollo.ast.GQLEnumValue
9-
import com.apollographql.apollo.ast.GQLEnumValueDefinition
10-
import com.apollographql.apollo.ast.GQLFloatValue
11-
import com.apollographql.apollo.ast.GQLInputObjectTypeDefinition
12-
import com.apollographql.apollo.ast.GQLInputValueDefinition
13-
import com.apollographql.apollo.ast.GQLIntValue
14-
import com.apollographql.apollo.ast.GQLListType
15-
import com.apollographql.apollo.ast.GQLListValue
16-
import com.apollographql.apollo.ast.GQLNamed
17-
import com.apollographql.apollo.ast.GQLNamedType
18-
import com.apollographql.apollo.ast.GQLNode
19-
import com.apollographql.apollo.ast.GQLNonNullType
20-
import com.apollographql.apollo.ast.GQLNullValue
21-
import com.apollographql.apollo.ast.GQLObjectValue
22-
import com.apollographql.apollo.ast.GQLScalarTypeDefinition
23-
import com.apollographql.apollo.ast.GQLStringValue
24-
import com.apollographql.apollo.ast.GQLVariableValue
25-
import com.apollographql.apollo.ast.toUtf8
3+
import com.apollographql.apollo.ast.*
264

275
/**
286
* Returns true if the two nodes are semantically equal, which ignores the source location and the description.
29-
* Note that not all cases are implemented - currently [GQLEnumTypeDefinition] and [GQLDirectiveDefinition] are fully supported, and
30-
* unsupported types will throw.
317
*/
328
internal fun GQLNode.semanticEquals(other: GQLNode?): Boolean {
339
if (other == null) return false
34-
when (this) {
35-
is GQLDirectiveDefinition -> {
36-
if (other !is GQLDirectiveDefinition) {
37-
return false
38-
}
3910

40-
if (locations != other.locations) {
41-
return false
42-
}
43-
44-
if (repeatable != other.repeatable) {
45-
return false
46-
}
47-
}
48-
49-
is GQLInputValueDefinition -> {
50-
if (other !is GQLInputValueDefinition) {
51-
return false
52-
}
53-
54-
if (!type.semanticEquals(other.type)) {
55-
return false
56-
}
57-
58-
if (defaultValue != null) {
59-
if (!defaultValue.semanticEquals(other.defaultValue)) {
60-
return false
61-
}
62-
} else if (other.defaultValue != null) {
63-
return false
64-
}
65-
}
66-
67-
is GQLNonNullType -> {
68-
if (other !is GQLNonNullType) {
69-
return false
70-
}
71-
}
72-
73-
is GQLListType -> {
74-
if (other !is GQLListType) {
75-
return false
76-
}
77-
}
78-
79-
is GQLNamedType -> {
80-
if (other !is GQLNamedType) {
81-
return false
82-
}
83-
}
84-
85-
is GQLNullValue -> {
86-
if (other !is GQLNullValue) {
87-
return false
88-
}
89-
}
90-
91-
is GQLListValue -> {
92-
if (other !is GQLListValue) {
93-
return false
94-
}
95-
}
96-
97-
is GQLObjectValue -> {
98-
if (other !is GQLObjectValue) {
99-
return false
100-
}
101-
}
102-
103-
is GQLStringValue -> {
104-
if (other !is GQLStringValue) {
105-
return false
106-
}
107-
if (value != other.value) {
108-
return false
109-
}
110-
}
111-
112-
is GQLBooleanValue -> {
113-
if (other !is GQLBooleanValue) {
114-
return false
115-
}
116-
if (value != other.value) {
117-
return false
118-
}
119-
}
120-
121-
is GQLIntValue -> {
122-
if (other !is GQLIntValue) {
123-
return false
124-
}
125-
if (value != other.value) {
126-
return false
127-
}
128-
}
129-
130-
is GQLFloatValue -> {
131-
if (other !is GQLFloatValue) {
132-
return false
133-
}
134-
if (value != other.value) {
135-
return false
136-
}
137-
}
138-
139-
is GQLEnumValue -> {
140-
if (other !is GQLEnumValue) {
141-
return false
142-
}
143-
if (value != other.value) {
144-
return false
145-
}
146-
}
147-
148-
is GQLVariableValue -> {
149-
if (other !is GQLVariableValue) {
150-
return false
151-
}
152-
}
153-
154-
is GQLEnumTypeDefinition -> {
155-
if (other !is GQLEnumTypeDefinition) {
156-
return false
157-
}
158-
}
159-
160-
is GQLDirective -> {
161-
if (other !is GQLDirective) {
162-
return false
163-
}
164-
}
165-
166-
is GQLArgument -> {
167-
if (other !is GQLArgument) {
168-
return false
169-
}
170-
}
171-
172-
is GQLEnumValueDefinition -> {
173-
if (other !is GQLEnumValueDefinition) {
174-
return false
175-
}
176-
}
177-
178-
is GQLInputObjectTypeDefinition -> {
179-
if (other !is GQLInputObjectTypeDefinition) {
180-
return false
181-
}
182-
}
183-
184-
is GQLScalarTypeDefinition -> {
185-
if (other !is GQLScalarTypeDefinition) {
186-
return false
187-
}
188-
}
189-
190-
else -> {
191-
TODO("semanticEquals not supported for ${this::class.simpleName}")
192-
}
193-
}
194-
195-
if (this is GQLNamed) {
196-
if (other !is GQLNamed) {
197-
return false
198-
}
199-
if (name != other.name) {
200-
return false
201-
}
202-
}
203-
204-
if (children.size != other.children.size) {
205-
return false
206-
}
207-
for (i in children.indices) {
208-
if (!children[i].semanticEquals(other.children[i])) {
209-
return false
210-
}
211-
}
212-
return true
213-
}
214-
215-
internal fun GQLDirectiveDefinition.toSemanticSdl(): String {
216-
return copy(description = null, arguments = arguments.map { it.copy(description = null) }).toUtf8().trim()
11+
return toSemanticSdl() == other.toSemanticSdl()
21712
}
21813

219-
internal fun GQLEnumTypeDefinition.toSemanticSdl(): String {
220-
return copy(description = null, enumValues = enumValues.map { it.copy(description = null) }).toUtf8().replace(Regex("[\\n ]+"), " ").trim()
14+
internal fun GQLNode.toSemanticSdl(): String {
15+
return transform2 { node ->
16+
when (node) {
17+
is GQLInputValueDefinition -> node.copy(description = null)
18+
is GQLObjectTypeDefinition -> node.copy(description = null)
19+
is GQLScalarTypeDefinition -> node.copy(description = null)
20+
is GQLInputObjectTypeDefinition -> node.copy(description = null)
21+
is GQLUnionTypeDefinition -> node.copy(description = null)
22+
is GQLEnumTypeDefinition -> node.copy(description = null)
23+
is GQLInterfaceTypeDefinition -> node.copy(description = null)
24+
is GQLFieldDefinition -> node.copy(description = null)
25+
is GQLDirectiveDefinition -> node.copy(description = null)
26+
is GQLEnumValueDefinition -> node.copy(description = null)
27+
is GQLFragmentDefinition -> node.copy(description = null)
28+
is GQLOperationDefinition -> node.copy(description = null)
29+
is GQLSchemaDefinition -> node.copy(description = null)
30+
else -> node
31+
}
32+
}!!.toUtf8("").replace(Regex("[\\n ]+"), " ").trim()
22133
}

0 commit comments

Comments
 (0)