-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds writers for Fields, OrderedElements, and logic constraints (#292)
- Loading branch information
Showing
10 changed files
with
264 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
ion-schema/src/main/kotlin/com/amazon/ionschema/writer/internal/constraints/FieldsWriter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package com.amazon.ionschema.writer.internal.constraints | ||
|
||
import com.amazon.ion.IonWriter | ||
import com.amazon.ionschema.IonSchemaVersion | ||
import com.amazon.ionschema.model.Constraint | ||
import com.amazon.ionschema.model.ExperimentalIonSchemaModel | ||
import com.amazon.ionschema.model.VariablyOccurringTypeArgument.Companion.OCCURS_OPTIONAL | ||
import com.amazon.ionschema.writer.internal.TypeWriter | ||
import com.amazon.ionschema.writer.internal.writeToStruct | ||
|
||
@ExperimentalIonSchemaModel | ||
internal class FieldsWriter(private val typeWriter: TypeWriter, private val ionSchemaVersion: IonSchemaVersion) : ConstraintWriter { | ||
override val supportedClasses = setOf(Constraint.Fields::class) | ||
|
||
override fun IonWriter.write(c: Constraint) { | ||
check(c is Constraint.Fields) | ||
|
||
if (c.closed && ionSchemaVersion == IonSchemaVersion.v1_0) { | ||
setFieldName("content") | ||
writeSymbol("closed") | ||
} | ||
|
||
setFieldName("fields") | ||
if (c.closed && ionSchemaVersion != IonSchemaVersion.v1_0) setTypeAnnotations("closed") | ||
writeToStruct(c.fields) { | ||
typeWriter.writeVariablyOccurringTypeArg(this@writeToStruct, it, elideOccursValue = OCCURS_OPTIONAL) | ||
} | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
...rc/main/kotlin/com/amazon/ionschema/writer/internal/constraints/LogicConstraintsWriter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package com.amazon.ionschema.writer.internal.constraints | ||
|
||
import com.amazon.ion.IonWriter | ||
import com.amazon.ionschema.model.Constraint | ||
import com.amazon.ionschema.model.ExperimentalIonSchemaModel | ||
import com.amazon.ionschema.writer.internal.TypeWriter | ||
import com.amazon.ionschema.writer.internal.writeTypeArguments | ||
import kotlin.reflect.KClass | ||
|
||
@ExperimentalIonSchemaModel | ||
internal class LogicConstraintsWriter(private val typeWriter: TypeWriter) : ConstraintWriter { | ||
override val supportedClasses: Set<KClass<out Constraint>> = setOf( | ||
Constraint.AllOf::class, | ||
Constraint.AnyOf::class, | ||
Constraint.Not::class, | ||
Constraint.OneOf::class, | ||
Constraint.Type::class, | ||
) | ||
|
||
override fun IonWriter.write(c: Constraint) { | ||
when (c) { | ||
is Constraint.AllOf -> { | ||
setFieldName("all_of") | ||
typeWriter.writeTypeArguments(this@write, c.types) | ||
} | ||
is Constraint.AnyOf -> { | ||
setFieldName("any_of") | ||
typeWriter.writeTypeArguments(this@write, c.types) | ||
} | ||
is Constraint.OneOf -> { | ||
setFieldName("one_of") | ||
typeWriter.writeTypeArguments(this@write, c.types) | ||
} | ||
is Constraint.Not -> { | ||
setFieldName("not") | ||
typeWriter.writeTypeArg(this@write, c.type) | ||
} | ||
is Constraint.Type -> { | ||
setFieldName("type") | ||
typeWriter.writeTypeArg(this@write, c.type) | ||
} | ||
else -> check(false) | ||
} | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
...src/main/kotlin/com/amazon/ionschema/writer/internal/constraints/OrderedElementsWriter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package com.amazon.ionschema.writer.internal.constraints | ||
|
||
import com.amazon.ion.IonWriter | ||
import com.amazon.ionschema.model.Constraint | ||
import com.amazon.ionschema.model.ExperimentalIonSchemaModel | ||
import com.amazon.ionschema.model.VariablyOccurringTypeArgument.Companion.OCCURS_REQUIRED | ||
import com.amazon.ionschema.writer.internal.TypeWriter | ||
import com.amazon.ionschema.writer.internal.writeToList | ||
|
||
@ExperimentalIonSchemaModel | ||
internal class OrderedElementsWriter(private val typeWriter: TypeWriter) : ConstraintWriter { | ||
override val supportedClasses = setOf(Constraint.OrderedElements::class) | ||
|
||
override fun IonWriter.write(c: Constraint) { | ||
check(c is Constraint.OrderedElements) | ||
setFieldName("ordered_elements") | ||
writeToList(c.types) { | ||
typeWriter.writeVariablyOccurringTypeArg(this, it, elideOccursValue = OCCURS_REQUIRED) | ||
} | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
...hema/src/test/kotlin/com/amazon/ionschema/writer/internal/constraints/FieldsWriterTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package com.amazon.ionschema.writer.internal.constraints | ||
|
||
import com.amazon.ionschema.IonSchemaVersion | ||
import com.amazon.ionschema.model.Constraint | ||
import com.amazon.ionschema.model.ExperimentalIonSchemaModel | ||
import com.amazon.ionschema.model.TypeArgument | ||
import com.amazon.ionschema.model.occurs | ||
import com.amazon.ionschema.model.optional | ||
import org.junit.jupiter.api.Test | ||
|
||
@OptIn(ExperimentalIonSchemaModel::class) | ||
class FieldsWriterTest : ConstraintTestBase( | ||
writer = FieldsWriter(stubTypeWriterWithRefs("foo_type", "bar_type"), IonSchemaVersion.v2_0), | ||
expectedConstraints = setOf(Constraint.Fields::class), | ||
writeTestCases = listOf( | ||
Constraint.Fields(fieldsMap, closed = true) to "fields: closed::{ a: foo_type, b: bar_type }", | ||
Constraint.Fields(fieldsMap, closed = false) to "fields: { a: foo_type, b: bar_type }", | ||
) | ||
) { | ||
companion object { | ||
private val fieldsMap = mapOf( | ||
"a" to TypeArgument.Reference("foo_type").optional(), | ||
"b" to TypeArgument.Reference("bar_type").occurs(0, 1), | ||
) | ||
} | ||
|
||
@Test | ||
fun `writer should write content closed for v1_0`() { | ||
val writer = FieldsWriter(stubTypeWriterWithRefs("foo_type", "bar_type"), IonSchemaVersion.v1_0) | ||
runWriteCase( | ||
writer, | ||
Constraint.Fields(fieldsMap, closed = true) to "content: closed, fields: { a: foo_type, b: bar_type }" | ||
) | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
...est/kotlin/com/amazon/ionschema/writer/internal/constraints/LogicConstraintsWriterTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package com.amazon.ionschema.writer.internal.constraints | ||
|
||
import com.amazon.ionschema.model.Constraint | ||
import com.amazon.ionschema.model.ExperimentalIonSchemaModel | ||
import com.amazon.ionschema.model.TypeArgument.Reference | ||
|
||
@OptIn(ExperimentalIonSchemaModel::class) | ||
class LogicConstraintsWriterTest : ConstraintTestBase( | ||
writer = LogicConstraintsWriter(stubTypeWriterWithRefs("foo", "bar")), | ||
expectedConstraints = setOf( | ||
Constraint.AllOf::class, | ||
Constraint.AnyOf::class, | ||
Constraint.OneOf::class, | ||
Constraint.Not::class, | ||
Constraint.Type::class, | ||
), | ||
writeTestCases = listOf( | ||
Constraint.AllOf(emptySet()) to "all_of: []", | ||
Constraint.AllOf(Reference("foo")) to "all_of: [foo]", | ||
Constraint.AllOf(Reference("foo"), Reference("bar")) to "all_of: [foo, bar]", | ||
Constraint.AnyOf(emptySet()) to "any_of: []", | ||
Constraint.AnyOf(Reference("foo")) to "any_of: [foo]", | ||
Constraint.AnyOf(Reference("foo"), Reference("bar")) to "any_of: [foo, bar]", | ||
Constraint.OneOf(emptySet()) to "one_of: []", | ||
Constraint.OneOf(Reference("foo")) to "one_of: [foo]", | ||
Constraint.OneOf(Reference("foo"), Reference("bar")) to "one_of: [foo, bar]", | ||
Constraint.Not(Reference("foo")) to "not: foo", | ||
Constraint.Type(Reference("foo")) to "type: foo", | ||
) | ||
) |
24 changes: 24 additions & 0 deletions
24
...test/kotlin/com/amazon/ionschema/writer/internal/constraints/OrderedElementsWriterTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package com.amazon.ionschema.writer.internal.constraints | ||
|
||
import com.amazon.ionschema.model.Constraint | ||
import com.amazon.ionschema.model.ExperimentalIonSchemaModel | ||
import com.amazon.ionschema.model.TypeArgument.Reference | ||
import com.amazon.ionschema.model.required | ||
|
||
@OptIn(ExperimentalIonSchemaModel::class) | ||
class OrderedElementsWriterTest : ConstraintTestBase( | ||
writer = OrderedElementsWriter(stubTypeWriterWithRefs("foo", "bar")), | ||
expectedConstraints = setOf(Constraint.OrderedElements::class), | ||
writeTestCases = listOf( | ||
Constraint.OrderedElements(emptyList()) to "ordered_elements: []", | ||
Constraint.OrderedElements(Reference("foo").required()) to "ordered_elements: [foo]", | ||
Constraint.OrderedElements( | ||
Reference("foo").required(), | ||
Reference("bar").required(), | ||
Reference("foo").required(), | ||
) to "ordered_elements: [foo, bar, foo]", | ||
) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters