-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
- Loading branch information
There are no files selected for viewing
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) | ||
} | ||
} | ||
} |
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, | ||
Check warning on line 20 in ion-schema/src/main/kotlin/com/amazon/ionschema/writer/internal/constraints/LogicConstraintsWriter.kt Codecov / codecov/patchion-schema/src/main/kotlin/com/amazon/ionschema/writer/internal/constraints/LogicConstraintsWriter.kt#L14-L20
|
||
) | ||
|
||
override fun IonWriter.write(c: Constraint) { | ||
when (c) { | ||
Check warning on line 24 in ion-schema/src/main/kotlin/com/amazon/ionschema/writer/internal/constraints/LogicConstraintsWriter.kt Codecov / codecov/patchion-schema/src/main/kotlin/com/amazon/ionschema/writer/internal/constraints/LogicConstraintsWriter.kt#L24
|
||
is Constraint.AllOf -> { | ||
setFieldName("all_of") | ||
typeWriter.writeTypeArguments(this@write, c.types) | ||
Check warning on line 27 in ion-schema/src/main/kotlin/com/amazon/ionschema/writer/internal/constraints/LogicConstraintsWriter.kt Codecov / codecov/patchion-schema/src/main/kotlin/com/amazon/ionschema/writer/internal/constraints/LogicConstraintsWriter.kt#L26-L27
|
||
} | ||
is Constraint.AnyOf -> { | ||
setFieldName("any_of") | ||
typeWriter.writeTypeArguments(this@write, c.types) | ||
Check warning on line 31 in ion-schema/src/main/kotlin/com/amazon/ionschema/writer/internal/constraints/LogicConstraintsWriter.kt Codecov / codecov/patchion-schema/src/main/kotlin/com/amazon/ionschema/writer/internal/constraints/LogicConstraintsWriter.kt#L30-L31
|
||
} | ||
is Constraint.OneOf -> { | ||
setFieldName("one_of") | ||
typeWriter.writeTypeArguments(this@write, c.types) | ||
Check warning on line 35 in ion-schema/src/main/kotlin/com/amazon/ionschema/writer/internal/constraints/LogicConstraintsWriter.kt Codecov / codecov/patchion-schema/src/main/kotlin/com/amazon/ionschema/writer/internal/constraints/LogicConstraintsWriter.kt#L34-L35
|
||
} | ||
is Constraint.Not -> { | ||
setFieldName("not") | ||
typeWriter.writeTypeArg(this@write, c.type) | ||
Check warning on line 39 in ion-schema/src/main/kotlin/com/amazon/ionschema/writer/internal/constraints/LogicConstraintsWriter.kt Codecov / codecov/patchion-schema/src/main/kotlin/com/amazon/ionschema/writer/internal/constraints/LogicConstraintsWriter.kt#L38-L39
|
||
} | ||
is Constraint.Type -> { | ||
setFieldName("type") | ||
typeWriter.writeTypeArg(this@write, c.type) | ||
Check warning on line 43 in ion-schema/src/main/kotlin/com/amazon/ionschema/writer/internal/constraints/LogicConstraintsWriter.kt Codecov / codecov/patchion-schema/src/main/kotlin/com/amazon/ionschema/writer/internal/constraints/LogicConstraintsWriter.kt#L42-L43
|
||
} | ||
else -> check(false) | ||
Check warning on line 45 in ion-schema/src/main/kotlin/com/amazon/ionschema/writer/internal/constraints/LogicConstraintsWriter.kt Codecov / codecov/patchion-schema/src/main/kotlin/com/amazon/ionschema/writer/internal/constraints/LogicConstraintsWriter.kt#L45
|
||
} | ||
} | ||
} |
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) | ||
Check warning on line 15 in ion-schema/src/main/kotlin/com/amazon/ionschema/writer/internal/constraints/OrderedElementsWriter.kt Codecov / codecov/patchion-schema/src/main/kotlin/com/amazon/ionschema/writer/internal/constraints/OrderedElementsWriter.kt#L14-L15
|
||
|
||
override fun IonWriter.write(c: Constraint) { | ||
check(c is Constraint.OrderedElements) | ||
setFieldName("ordered_elements") | ||
writeToList(c.types) { | ||
typeWriter.writeVariablyOccurringTypeArg(this, it, elideOccursValue = OCCURS_REQUIRED) | ||
} | ||
Check warning on line 22 in ion-schema/src/main/kotlin/com/amazon/ionschema/writer/internal/constraints/OrderedElementsWriter.kt Codecov / codecov/patchion-schema/src/main/kotlin/com/amazon/ionschema/writer/internal/constraints/OrderedElementsWriter.kt#L19-L22
|
||
} | ||
} |
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 }" | ||
) | ||
} | ||
} |
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", | ||
) | ||
) |
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]", | ||
) | ||
) |