Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds writers for remaining constraints #293

Merged
merged 1 commit into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// 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.Constraint.AnnotationsV2.Modifier.Closed
import com.amazon.ionschema.model.Constraint.AnnotationsV2.Modifier.Exact
import com.amazon.ionschema.model.Constraint.AnnotationsV2.Modifier.Required
import com.amazon.ionschema.model.ExperimentalIonSchemaModel
import com.amazon.ionschema.writer.internal.TypeWriter
import com.amazon.ionschema.writer.internal.writeToList

@ExperimentalIonSchemaModel
internal class AnnotationsV2Writer(private val typeWriter: TypeWriter) : ConstraintWriter {

Check warning on line 16 in ion-schema/src/main/kotlin/com/amazon/ionschema/writer/internal/constraints/AnnotationsV2Writer.kt

View check run for this annotation

Codecov / codecov/patch

ion-schema/src/main/kotlin/com/amazon/ionschema/writer/internal/constraints/AnnotationsV2Writer.kt#L16

Added line #L16 was not covered by tests

override val supportedClasses = setOf(
Constraint.AnnotationsV2.Simplified::class,
Constraint.AnnotationsV2.Standard::class

Check warning on line 20 in ion-schema/src/main/kotlin/com/amazon/ionschema/writer/internal/constraints/AnnotationsV2Writer.kt

View check run for this annotation

Codecov / codecov/patch

ion-schema/src/main/kotlin/com/amazon/ionschema/writer/internal/constraints/AnnotationsV2Writer.kt#L18-L20

Added lines #L18 - L20 were not covered by tests
)

override fun IonWriter.write(c: Constraint) {
check(c is Constraint.AnnotationsV2)

setFieldName("annotations")
when (c) {

Check warning on line 27 in ion-schema/src/main/kotlin/com/amazon/ionschema/writer/internal/constraints/AnnotationsV2Writer.kt

View check run for this annotation

Codecov / codecov/patch

ion-schema/src/main/kotlin/com/amazon/ionschema/writer/internal/constraints/AnnotationsV2Writer.kt#L26-L27

Added lines #L26 - L27 were not covered by tests
is Constraint.AnnotationsV2.Standard -> typeWriter.writeTypeArg(this, c.type)
is Constraint.AnnotationsV2.Simplified -> {
when (c.modifier) {
Closed -> setTypeAnnotations("closed")
Required -> setTypeAnnotations("required")
Exact -> setTypeAnnotations("closed", "required")

Check warning on line 33 in ion-schema/src/main/kotlin/com/amazon/ionschema/writer/internal/constraints/AnnotationsV2Writer.kt

View check run for this annotation

Codecov / codecov/patch

ion-schema/src/main/kotlin/com/amazon/ionschema/writer/internal/constraints/AnnotationsV2Writer.kt#L31-L33

Added lines #L31 - L33 were not covered by tests
}
writeToList(c.annotations) { writeSymbol(it) }

Check warning on line 35 in ion-schema/src/main/kotlin/com/amazon/ionschema/writer/internal/constraints/AnnotationsV2Writer.kt

View check run for this annotation

Codecov / codecov/patch

ion-schema/src/main/kotlin/com/amazon/ionschema/writer/internal/constraints/AnnotationsV2Writer.kt#L35

Added line #L35 was not covered by tests
}
}

Check warning on line 37 in ion-schema/src/main/kotlin/com/amazon/ionschema/writer/internal/constraints/AnnotationsV2Writer.kt

View check run for this annotation

Codecov / codecov/patch

ion-schema/src/main/kotlin/com/amazon/ionschema/writer/internal/constraints/AnnotationsV2Writer.kt#L37

Added line #L37 was not covered by tests
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// 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.writeIonValue
import com.amazon.ionschema.writer.internal.writeToList

@ExperimentalIonSchemaModel
internal object ContainsWriter : ConstraintWriter {
override val supportedClasses = setOf(Constraint.Contains::class)

Check warning on line 14 in ion-schema/src/main/kotlin/com/amazon/ionschema/writer/internal/constraints/ContainsWriter.kt

View check run for this annotation

Codecov / codecov/patch

ion-schema/src/main/kotlin/com/amazon/ionschema/writer/internal/constraints/ContainsWriter.kt#L14

Added line #L14 was not covered by tests

override fun IonWriter.write(c: Constraint) {
check(c is Constraint.Contains)
setFieldName("contains")
writeToList(c.values) { writeIonValue(it) }

Check warning on line 19 in ion-schema/src/main/kotlin/com/amazon/ionschema/writer/internal/constraints/ContainsWriter.kt

View check run for this annotation

Codecov / codecov/patch

ion-schema/src/main/kotlin/com/amazon/ionschema/writer/internal/constraints/ContainsWriter.kt#L18-L19

Added lines #L18 - L19 were not covered by tests
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// 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.IonSchemaException
import com.amazon.ionschema.IonSchemaVersion
import com.amazon.ionschema.model.Constraint
import com.amazon.ionschema.model.ExperimentalIonSchemaModel
import com.amazon.ionschema.writer.internal.TypeWriter

@ExperimentalIonSchemaModel
internal class ElementWriter(private val typeWriter: TypeWriter, private val ionSchemaVersion: IonSchemaVersion) : ConstraintWriter {
override val supportedClasses = setOf(Constraint.Element::class)

override fun IonWriter.write(c: Constraint) {
check(c is Constraint.Element)

setFieldName("element")
if (c.distinct) {
if (ionSchemaVersion == IonSchemaVersion.v1_0) {
throw IonSchemaException("Ion Schema 1.0 does not support 'distinct' elements")
} else {
setTypeAnnotations("distinct")
}

Check warning on line 26 in ion-schema/src/main/kotlin/com/amazon/ionschema/writer/internal/constraints/ElementWriter.kt

View check run for this annotation

Codecov / codecov/patch

ion-schema/src/main/kotlin/com/amazon/ionschema/writer/internal/constraints/ElementWriter.kt#L25-L26

Added lines #L25 - L26 were not covered by tests
}
typeWriter.writeTypeArg(this@write, c.type)

Check warning on line 28 in ion-schema/src/main/kotlin/com/amazon/ionschema/writer/internal/constraints/ElementWriter.kt

View check run for this annotation

Codecov / codecov/patch

ion-schema/src/main/kotlin/com/amazon/ionschema/writer/internal/constraints/ElementWriter.kt#L28

Added line #L28 was not covered by tests
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// 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

@ExperimentalIonSchemaModel
internal class FieldNamesWriter(private val typeWriter: TypeWriter) : ConstraintWriter {
override val supportedClasses = setOf(Constraint.FieldNames::class)

Check warning on line 13 in ion-schema/src/main/kotlin/com/amazon/ionschema/writer/internal/constraints/FieldNamesWriter.kt

View check run for this annotation

Codecov / codecov/patch

ion-schema/src/main/kotlin/com/amazon/ionschema/writer/internal/constraints/FieldNamesWriter.kt#L12-L13

Added lines #L12 - L13 were not covered by tests

override fun IonWriter.write(c: Constraint) {
check(c is Constraint.FieldNames)
setFieldName("field_names")

Check warning on line 17 in ion-schema/src/main/kotlin/com/amazon/ionschema/writer/internal/constraints/FieldNamesWriter.kt

View check run for this annotation

Codecov / codecov/patch

ion-schema/src/main/kotlin/com/amazon/ionschema/writer/internal/constraints/FieldNamesWriter.kt#L17

Added line #L17 was not covered by tests
if (c.distinct) {
setTypeAnnotations("distinct")

Check warning on line 19 in ion-schema/src/main/kotlin/com/amazon/ionschema/writer/internal/constraints/FieldNamesWriter.kt

View check run for this annotation

Codecov / codecov/patch

ion-schema/src/main/kotlin/com/amazon/ionschema/writer/internal/constraints/FieldNamesWriter.kt#L19

Added line #L19 was not covered by tests
}
typeWriter.writeTypeArg(this@write, c.type)

Check warning on line 21 in ion-schema/src/main/kotlin/com/amazon/ionschema/writer/internal/constraints/FieldNamesWriter.kt

View check run for this annotation

Codecov / codecov/patch

ion-schema/src/main/kotlin/com/amazon/ionschema/writer/internal/constraints/FieldNamesWriter.kt#L21

Added line #L21 was not covered by tests
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// 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

@ExperimentalIonSchemaModel
object Ieee754FloatWriter : ConstraintWriter {
override val supportedClasses = setOf(Constraint.Ieee754Float::class)

Check warning on line 12 in ion-schema/src/main/kotlin/com/amazon/ionschema/writer/internal/constraints/Ieee754FloatWriter.kt

View check run for this annotation

Codecov / codecov/patch

ion-schema/src/main/kotlin/com/amazon/ionschema/writer/internal/constraints/Ieee754FloatWriter.kt#L12

Added line #L12 was not covered by tests

override fun IonWriter.write(c: Constraint) {
check(c is Constraint.Ieee754Float)
setFieldName("ieee754_float")
writeSymbol(c.format.symbolText)

Check warning on line 17 in ion-schema/src/main/kotlin/com/amazon/ionschema/writer/internal/constraints/Ieee754FloatWriter.kt

View check run for this annotation

Codecov / codecov/patch

ion-schema/src/main/kotlin/com/amazon/ionschema/writer/internal/constraints/Ieee754FloatWriter.kt#L16-L17

Added lines #L16 - L17 were not covered by tests
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// 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

@ExperimentalIonSchemaModel
internal object RegexWriter : ConstraintWriter {
override val supportedClasses = setOf(Constraint.Regex::class)

Check warning on line 12 in ion-schema/src/main/kotlin/com/amazon/ionschema/writer/internal/constraints/RegexWriter.kt

View check run for this annotation

Codecov / codecov/patch

ion-schema/src/main/kotlin/com/amazon/ionschema/writer/internal/constraints/RegexWriter.kt#L12

Added line #L12 was not covered by tests

override fun IonWriter.write(c: Constraint) {
check(c is Constraint.Regex)
setFieldName("regex")

Check warning on line 16 in ion-schema/src/main/kotlin/com/amazon/ionschema/writer/internal/constraints/RegexWriter.kt

View check run for this annotation

Codecov / codecov/patch

ion-schema/src/main/kotlin/com/amazon/ionschema/writer/internal/constraints/RegexWriter.kt#L16

Added line #L16 was not covered by tests
if (c.caseInsensitive) addTypeAnnotation("i")
if (c.multiline) addTypeAnnotation("m")
writeString(c.pattern)

Check warning on line 19 in ion-schema/src/main/kotlin/com/amazon/ionschema/writer/internal/constraints/RegexWriter.kt

View check run for this annotation

Codecov / codecov/patch

ion-schema/src/main/kotlin/com/amazon/ionschema/writer/internal/constraints/RegexWriter.kt#L19

Added line #L19 was not covered by tests
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// 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.writeToList

@ExperimentalIonSchemaModel
internal object TimestampOffsetWriter : ConstraintWriter {
override val supportedClasses = setOf(Constraint.TimestampOffset::class)

Check warning on line 13 in ion-schema/src/main/kotlin/com/amazon/ionschema/writer/internal/constraints/TimestampOffsetWriter.kt

View check run for this annotation

Codecov / codecov/patch

ion-schema/src/main/kotlin/com/amazon/ionschema/writer/internal/constraints/TimestampOffsetWriter.kt#L13

Added line #L13 was not covered by tests

override fun IonWriter.write(c: Constraint) {
check(c is Constraint.TimestampOffset)
setFieldName("timestamp_offset")
writeToList(c.offsets) { writeString(it.toString()) }

Check warning on line 18 in ion-schema/src/main/kotlin/com/amazon/ionschema/writer/internal/constraints/TimestampOffsetWriter.kt

View check run for this annotation

Codecov / codecov/patch

ion-schema/src/main/kotlin/com/amazon/ionschema/writer/internal/constraints/TimestampOffsetWriter.kt#L17-L18

Added lines #L17 - L18 were not covered by tests
}
}
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.AnnotationsV2
import com.amazon.ionschema.model.Constraint.AnnotationsV2.Modifier
import com.amazon.ionschema.model.ExperimentalIonSchemaModel
import com.amazon.ionschema.model.TypeArgument

@OptIn(ExperimentalIonSchemaModel::class)
class AnnotationsV2WriterTest : ConstraintTestBase(
writer = AnnotationsV2Writer(stubTypeWriterWithRefs("foo_type")),
expectedConstraints = setOf(
AnnotationsV2.Simplified::class,
AnnotationsV2.Standard::class,
),
writeTestCases = listOf(
AnnotationsV2.Standard(TypeArgument.Reference("foo_type")) to "annotations: foo_type",
AnnotationsV2.Simplified(Modifier.Closed, setOf("a")) to "annotations: closed::[a]",
AnnotationsV2.Simplified(Modifier.Required, setOf("b")) to "annotations: required::[b]",
AnnotationsV2.Simplified(Modifier.Exact, setOf("c")) to "annotations: closed::required::[c]",
)
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// 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.Contains
import com.amazon.ionschema.model.ExperimentalIonSchemaModel

@OptIn(ExperimentalIonSchemaModel::class)
class ContainsWriterTest : ConstraintTestBase(
writer = ContainsWriter,
expectedConstraints = setOf(Contains::class),
writeTestCases = listOf(
Contains(emptySet()) to "contains: []",
Contains(setOf(ion("[foo, true]"), ion("bar"))) to "contains: [[foo, true], bar]",
Contains(setOf(ion("me::2"), ion("null.timestamp"), ion("{a:b}"))) to "contains: [me::2, null.timestamp, {a:b}]",
)
)
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.ionschema.IonSchemaException
import com.amazon.ionschema.IonSchemaVersion
import com.amazon.ionschema.model.Constraint
import com.amazon.ionschema.model.ExperimentalIonSchemaModel
import com.amazon.ionschema.model.TypeArgument
import io.mockk.mockk
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows

@OptIn(ExperimentalIonSchemaModel::class)
class ElementWriterTest : ConstraintTestBase(
writer = ElementWriter(stubTypeWriterWithRefs("foo_type"), IonSchemaVersion.v2_0),
expectedConstraints = setOf(Constraint.Element::class),
writeTestCases = listOf(
Constraint.Element(TypeArgument.Reference("foo_type")) to "element: foo_type",
Constraint.Element(TypeArgument.Reference("foo_type"), distinct = true) to "element: distinct::foo_type",
)
) {
@Test
fun `writer should throw exception when distinct = true and version = v1_0`() {
val writer = ElementWriter(stubTypeWriterWithRefs("foo_type"), IonSchemaVersion.v1_0)
val constraint = Constraint.Element(TypeArgument.Reference("foo_type"), distinct = true)
assertThrows<IonSchemaException> {
writer.writeTo(mockk(relaxed = true), constraint)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// 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

@OptIn(ExperimentalIonSchemaModel::class)
class FieldNamesWriterTest : ConstraintTestBase(
writer = FieldNamesWriter(stubTypeWriterWithRefs("foo_type")),
expectedConstraints = setOf(Constraint.FieldNames::class),
writeTestCases = listOf(
Constraint.FieldNames(TypeArgument.Reference("foo_type")) to "field_names: foo_type",
Constraint.FieldNames(TypeArgument.Reference("foo_type"), distinct = true) to "field_names: distinct::foo_type",
)
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// 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.Ieee754InterchangeFormat

@OptIn(ExperimentalIonSchemaModel::class)
class Ieee754FloatWriterTest : ConstraintTestBase(
writer = Ieee754FloatWriter,
expectedConstraints = setOf(Constraint.Ieee754Float::class),
writeTestCases = listOf(
Constraint.Ieee754Float(Ieee754InterchangeFormat.Binary16) to "ieee754_float: binary16",
Constraint.Ieee754Float(Ieee754InterchangeFormat.Binary32) to "ieee754_float: binary32",
Constraint.Ieee754Float(Ieee754InterchangeFormat.Binary64) to "ieee754_float: binary64",
)
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// 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

@OptIn(ExperimentalIonSchemaModel::class)
class RegexWriterTest : ConstraintTestBase(
writer = RegexWriter,
expectedConstraints = setOf(Constraint.Regex::class),
writeTestCases = listOf(
Constraint.Regex("abc") to """ regex: "abc" """,
Constraint.Regex("abc", multiline = true) to """ regex: m::"abc" """,
Constraint.Regex("abc", caseInsensitive = true) to """ regex: i::"abc" """,
Constraint.Regex("abc", true, true) to """ regex: i::m::"abc" """,
)
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// 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.TimestampOffset
import com.amazon.ionschema.model.ExperimentalIonSchemaModel
import com.amazon.ionschema.model.TimestampOffsetValue.Companion.parse

@OptIn(ExperimentalIonSchemaModel::class)
class TimestampOffsetWriterTest : ConstraintTestBase(
writer = TimestampOffsetWriter,
expectedConstraints = setOf(TimestampOffset::class),
writeTestCases = listOf(
TimestampOffset(emptySet()) to "timestamp_offset: []",
TimestampOffset(setOf(parse("+01:23"))) to """ timestamp_offset: ["+01:23"] """,
TimestampOffset(setOf(parse("+01:23"), parse("-04:56"))) to """ timestamp_offset: ["+01:23", "-04:56"] """,
)
)
Loading