Skip to content

Commit c2957eb

Browse files
authored
Deprecate buildStruct() in favor of idiomatic ionStructOf() overloads (#99)
1 parent a066ae9 commit c2957eb

File tree

11 files changed

+92
-34
lines changed

11 files changed

+92
-34
lines changed

api/IonElement.api

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,13 +301,19 @@ public final class com/amazon/ionelement/api/Ion {
301301
public static final fun ionStructOf (Ljava/lang/Iterable;Ljava/util/List;)Lcom/amazon/ionelement/api/StructElement;
302302
public static final fun ionStructOf (Ljava/lang/Iterable;Ljava/util/List;Ljava/util/Map;)Lcom/amazon/ionelement/api/StructElement;
303303
public static final fun ionStructOf (Ljava/lang/Iterable;Ljava/util/Map;)Lcom/amazon/ionelement/api/StructElement;
304+
public static final fun ionStructOf (Ljava/util/List;Ljava/util/Map;Ljava/util/function/Consumer;)Lcom/amazon/ionelement/api/StructElement;
305+
public static final synthetic fun ionStructOf (Ljava/util/List;Ljava/util/Map;Lkotlin/jvm/functions/Function1;)Lcom/amazon/ionelement/api/StructElement;
306+
public static final fun ionStructOf (Ljava/util/List;Ljava/util/function/Consumer;)Lcom/amazon/ionelement/api/StructElement;
307+
public static final fun ionStructOf (Ljava/util/function/Consumer;)Lcom/amazon/ionelement/api/StructElement;
304308
public static final fun ionStructOf ([Lcom/amazon/ionelement/api/StructField;)Lcom/amazon/ionelement/api/StructElement;
305309
public static final fun ionStructOf ([Lcom/amazon/ionelement/api/StructField;Ljava/util/List;)Lcom/amazon/ionelement/api/StructElement;
306310
public static final fun ionStructOf ([Lcom/amazon/ionelement/api/StructField;Ljava/util/List;Ljava/util/Map;)Lcom/amazon/ionelement/api/StructElement;
307311
public static final fun ionStructOf ([Lkotlin/Pair;)Lcom/amazon/ionelement/api/StructElement;
308312
public static final fun ionStructOf ([Lkotlin/Pair;Ljava/util/List;)Lcom/amazon/ionelement/api/StructElement;
309313
public static final fun ionStructOf ([Lkotlin/Pair;Ljava/util/List;Ljava/util/Map;)Lcom/amazon/ionelement/api/StructElement;
310314
public static synthetic fun ionStructOf$default (Ljava/lang/Iterable;Ljava/util/List;Ljava/util/Map;ILjava/lang/Object;)Lcom/amazon/ionelement/api/StructElement;
315+
public static synthetic fun ionStructOf$default (Ljava/util/List;Ljava/util/Map;Ljava/util/function/Consumer;ILjava/lang/Object;)Lcom/amazon/ionelement/api/StructElement;
316+
public static synthetic fun ionStructOf$default (Ljava/util/List;Ljava/util/Map;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)Lcom/amazon/ionelement/api/StructElement;
311317
public static synthetic fun ionStructOf$default ([Lcom/amazon/ionelement/api/StructField;Ljava/util/List;Ljava/util/Map;ILjava/lang/Object;)Lcom/amazon/ionelement/api/StructElement;
312318
public static synthetic fun ionStructOf$default ([Lkotlin/Pair;Ljava/util/List;Ljava/util/Map;ILjava/lang/Object;)Lcom/amazon/ionelement/api/StructElement;
313319
public static final fun ionSymbol (Ljava/lang/String;)Lcom/amazon/ionelement/api/SymbolElement;

src/main/kotlin/com/amazon/ionelement/api/Ion.kt

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package com.amazon.ionelement.api
1818

1919
import com.amazon.ion.Decimal
2020
import com.amazon.ion.Timestamp
21+
import com.amazon.ionelement.impl.*
2122
import com.amazon.ionelement.impl.BigIntIntElementImpl
2223
import com.amazon.ionelement.impl.BlobElementImpl
2324
import com.amazon.ionelement.impl.BoolElementImpl
@@ -34,6 +35,7 @@ import com.amazon.ionelement.impl.StructFieldImpl
3435
import com.amazon.ionelement.impl.SymbolElementImpl
3536
import com.amazon.ionelement.impl.TimestampElementImpl
3637
import java.math.BigInteger
38+
import java.util.function.Consumer
3739
import kotlinx.collections.immutable.PersistentList
3840
import kotlinx.collections.immutable.persistentListOf
3941
import kotlinx.collections.immutable.toPersistentList
@@ -472,7 +474,45 @@ public fun ionStructOf(
472474
metas
473475
)
474476

477+
@JvmSynthetic
478+
public fun ionStructOf(
479+
annotations: Annotations = emptyList(),
480+
metas: MetaContainer = emptyMetaContainer(),
481+
builder: MutableStructFields.() -> Unit
482+
): StructElement {
483+
return ionStructOf(emptyIonStruct().mutableFields().apply(builder), annotations, metas)
484+
}
485+
486+
@JvmOverloads
487+
public fun ionStructOf(
488+
annotations: Annotations = emptyList(),
489+
metas: MetaContainer = emptyMetaContainer(),
490+
builder: Consumer<MutableStructFields>
491+
): StructElement {
492+
val fields = emptyIonStruct().mutableFields()
493+
builder.accept(fields)
494+
return ionStructOf(fields, annotations, metas)
495+
}
496+
497+
@Deprecated("Use `com.amazon.ionelement.api.Ion.ionStructOf()` instead")
475498
@JvmOverloads
499+
/**
500+
* For Kotlin, replace with:
501+
* ```
502+
* fun ionStructOf(
503+
* annotations: Annotations = emptyList(),
504+
* metas: MetaContainer = emptyMetaContainer(),
505+
* builder: MutableStructFields.() -> Unit
506+
* ): StructElement
507+
* ```
508+
*
509+
* For Java, replace with one of:
510+
* ```
511+
* StructElement ionStructOf(Consumer<MutableStructFields> builder)
512+
* StructElement ionStructOf(List<String> annotations, Consumer<MutableStructFields> builder)
513+
* StructElement ionStructOf(List<String> annotations, Map<String, Object> metas, Consumer<MutableStructFields> builder)
514+
* ```
515+
*/
476516
public fun buildStruct(
477517
annotations: Annotations = emptyList(),
478518
metas: MetaContainer = emptyMetaContainer(),

src/main/kotlin/com/amazon/ionelement/api/MutableStructFields.kt

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,24 +45,32 @@ public interface MutableStructFields : MutableCollection<StructField> {
4545
*/
4646
public fun add(fieldName: String, value: IonElement): Boolean
4747

48-
/** Adds the given field to the collection. The collection may have multiple fields with the same name.
48+
/**
49+
* Adds the given field to the collection. The collection may have multiple fields with the same name.
4950
*
50-
* Repeated fields are allowed, so this will always return true. */
51+
* Repeated fields are allowed, so this will always return true.
52+
*/
5153
public override fun add(element: StructField): Boolean
5254

53-
/** Removes a random occurrence of a field the matches the given field, or does nothing if no field exists.
55+
/**
56+
* Removes an arbitrary occurrence of a field the matches the given field, or does nothing if no field exists.
5457
*
55-
* Returns true is a field was removed. */
58+
* Returns true is a field was removed.
59+
*/
5660
public override fun remove(element: StructField): Boolean
5761

58-
/** Removes all occurrence of a field the matches the given field name, or does nothing if no field exists.
62+
/**
63+
* Removes all occurrences of a field the matches the given field name, or does nothing if no field exists.
5964
*
60-
* Returns true if a field was removed. */
65+
* Returns true if a field was removed.
66+
*/
6167
public fun clearField(fieldName: String): Boolean
6268

63-
/** Removes all of this collection's elements that are also contained in the specified collection.
69+
/**
70+
* Removes all of this collection's elements that are also contained in the specified collection.
6471
*
65-
* At most one field per element in the give collection is removed. */
72+
* At most one field per element in the give collection is removed.
73+
*/
6674
public override fun removeAll(elements: Collection<StructField>): Boolean
6775

6876
/** Adds all the given fields to the collection */

src/test/java/com/amazon/ionelement/demos/ConstructionDemo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.amazon.ionelement.demos.java;
1+
package com.amazon.ionelement.demos;
22

33
import com.amazon.ion.Decimal;
44
import com.amazon.ion.Timestamp;

src/test/java/com/amazon/ionelement/demos/DemoBase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.amazon.ionelement.demos.java;
1+
package com.amazon.ionelement.demos;
22

33
import com.amazon.ionelement.api.AnyElement;
44
import com.amazon.ionelement.api.ElementLoader;

src/test/java/com/amazon/ionelement/demos/ElementLoaderDemo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.amazon.ionelement.demos.java;
1+
package com.amazon.ionelement.demos;
22

33
import com.amazon.ion.IonReader;
44
import com.amazon.ionelement.api.AnyElement;

src/test/java/com/amazon/ionelement/demos/MutableStructFieldsDemo.java renamed to src/test/java/com/amazon/ionelement/demos/MutableStructFieldsJavaDemo.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,28 @@
1-
package com.amazon.ionelement.demos.java;
1+
package com.amazon.ionelement.demos;
22

33
import com.amazon.ionelement.api.Ion;
44
import com.amazon.ionelement.api.StructElement;
5-
import kotlin.Unit;
65
import org.junit.jupiter.api.Test;
76

87
import static com.amazon.ionelement.api.ElementLoader.loadSingleElement;
98
import static org.junit.jupiter.api.Assertions.assertEquals;
109

11-
public class MutableStructFieldsDemo {
10+
public class MutableStructFieldsJavaDemo {
1211

1312
@Test
1413
void createUpdatedStructFromExistingStruct() {
1514
StructElement original = loadSingleElement(
1615
"{name:\"Alice\",scores:{darts:100,billiards:15,}}").asStruct();
1716

18-
StructElement expected = loadSingleElement(
19-
"{name:\"Alice\",scores:{darts:100,billiards:30,pingPong:200,}}").asStruct();
17+
StructElement expected = Ion.ionStructOf(a -> {
18+
a.add("name", Ion.ionString("Alice"));
19+
a.add("scores", Ion.ionStructOf(b -> {
20+
b.add("darts", Ion.ionInt(100));
21+
b.add("billiards", Ion.ionInt(30));
22+
b.add("pingPong", Ion.ionInt(200));
23+
}
24+
));
25+
});
2026

2127
StructElement updated = original.update(fields ->
2228
fields.set("scores",

src/test/kotlin/com/amazon/ionelement/demos/AnyElementAccessorsDemo.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* permissions and limitations under the License.
1414
*/
1515

16-
package com.amazon.ionelement.demos.kotlin
16+
package com.amazon.ionelement.demos
1717

1818
import com.amazon.ion.Decimal
1919
import com.amazon.ionelement.api.AnyElement

src/test/kotlin/com/amazon/ionelement/demos/MutableStructFieldsDemo.kt renamed to src/test/kotlin/com/amazon/ionelement/demos/MutableStructFieldsKotlinDemo.kt

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
package com.amazon.ionelement.demos.kotlin
1+
package com.amazon.ionelement.demos
22

3-
import com.amazon.ionelement.api.ionInt
4-
import com.amazon.ionelement.api.loadSingleElement
3+
import com.amazon.ionelement.api.*
54
import kotlin.test.assertEquals
65
import org.junit.jupiter.api.Test
76

8-
class MutableStructFieldsDemo {
7+
class MutableStructFieldsKotlinDemo {
98
@Test
109
fun `create updated struct from existing struct`() {
1110
val original = loadSingleElement(
@@ -20,18 +19,17 @@ class MutableStructFieldsDemo {
2019
""".trimIndent()
2120
).asStruct()
2221

23-
val expected = loadSingleElement(
24-
"""
25-
{
26-
name: "Alice",
27-
scores: {
28-
darts: 100,
29-
billiards: 30,
30-
pingPong: 200,
31-
}
22+
val expected = ionStructOf {
23+
add("name", ionString("Alice"))
24+
add(
25+
"scores",
26+
ionStructOf {
27+
add("darts", ionInt(100))
28+
add("billiards", ionInt(30))
29+
add("pingPong", ionInt(200))
3230
}
33-
""".trimIndent()
34-
).asStruct()
31+
)
32+
}
3533

3634
val updated = original.update {
3735
set(

src/test/kotlin/com/amazon/ionelement/demos/NarrowingDemo.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.amazon.ionelement.demos.kotlin
1+
package com.amazon.ionelement.demos
22

33
import com.amazon.ionelement.api.IntElement
44
import com.amazon.ionelement.api.IonElement

0 commit comments

Comments
 (0)