Skip to content

Commit 712218b

Browse files
committed
Release 0.33.7
Minor changes to Json Signed-off-by: Gopal S Akshintala <[email protected]>
1 parent 635059e commit 712218b

File tree

15 files changed

+287
-51
lines changed

15 files changed

+287
-51
lines changed

.idea/misc.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ endif::[]
1616
:pmtemplates: src/integrationTest/resources/pm-templates
1717
:imagesdir: docs/images
1818
:prewrap!:
19-
:revoman-version: 0.33.6
19+
:revoman-version: 0.33.7
2020

2121
'''
2222

build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ dependencies {
3333
compileOnly(libs.immutables.value.annotations)
3434
compileOnly(libs.jetbrains.annotations)
3535
testImplementation(libs.truth)
36+
testImplementation(libs.json.assert)
3637
}
3738

3839
testing {

buildSrc/src/main/kotlin/Config.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
* ************************************************************************************************
77
*/
88
const val GROUP_ID = "com.salesforce.revoman"
9-
const val VERSION = "0.33.6"
9+
const val VERSION = "0.33.7"
1010
const val ARTIFACT_ID = "revoman"
1111
const val STAGING_PROFILE_ID = "1ea0a23e61ba7d"

src/integrationTest/java/com/salesforce/revoman/integration/core/pq/adapters/ConnectInputRepWithGraphAdapter.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import com.squareup.moshi.JsonReader;
2828
import com.squareup.moshi.JsonWriter;
2929
import com.squareup.moshi.Moshi;
30-
import io.vavr.control.Try;
3130
import java.lang.annotation.Annotation;
3231
import java.lang.reflect.Type;
3332
import java.util.Set;
@@ -119,7 +118,12 @@ public void toJson(@NotNull JsonWriter writer, T connectInputRepWithGraph) {
119118
connectInputRepWithGraph,
120119
writer,
121120
cirwg -> {
122-
writeProps(writer, pojoType, cirwg, Set.of(ObjectGraphInputRepresentation.class), moshi);
121+
writeProps(
122+
writer,
123+
pojoType,
124+
cirwg,
125+
Set.of(ObjectGraphInputRepresentation.class),
126+
dynamicJsonAdapter);
123127
objW(
124128
"graph",
125129
cirwg.getGraph(),
@@ -136,12 +140,8 @@ public void toJson(@NotNull JsonWriter writer, T connectInputRepWithGraph) {
136140
writer,
137141
rec -> {
138142
string("referenceId", rec.getReferenceId(), writer);
139-
Try.run(
140-
() -> {
141-
writer.name("record");
142-
dynamicJsonAdapter.toJson(
143-
writer, rec.getRecord().getRecordBody());
144-
});
143+
writer.name("record");
144+
dynamicJsonAdapter.toJson(writer, rec.getRecord().getRecordBody());
145145
}));
146146
});
147147
});
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* ************************************************************************************************
3+
* Copyright (c) 2023, Salesforce, Inc. All rights reserved. SPDX-License-Identifier: Apache License
4+
* Version 2.0 For full license text, see the LICENSE file in the repo root or
5+
* http://www.apache.org/licenses/LICENSE-2.0
6+
* ************************************************************************************************
7+
*/
8+
package com.salesforce.revoman.input.json
9+
10+
fun interface NestedNodeWriter<T> {
11+
@Throws(Throwable::class) fun write(t: T)
12+
}
13+
14+
fun interface NestedNodeReader<T> {
15+
@Throws(Throwable::class) fun read(t: T, s: String)
16+
}

src/main/kotlin/com/salesforce/revoman/input/json/JsonReaderUtils.kt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ package com.salesforce.revoman.input.json
1212
import com.squareup.moshi.JsonAdapter
1313
import com.squareup.moshi.JsonReader
1414
import com.squareup.moshi.Moshi
15-
import java.util.function.BiConsumer
1615
import org.springframework.beans.BeanUtils
1716

1817
fun nextString(reader: JsonReader): String = reader.nextString()
@@ -27,22 +26,22 @@ fun skipValue(reader: JsonReader) = reader.skipValue()
2726

2827
fun nextName(reader: JsonReader): String = reader.nextName()
2928

30-
fun <T> objR(mk: () -> T, reader: JsonReader, block: BiConsumer<T, String>): T =
29+
fun <T> objR(mk: () -> T, reader: JsonReader, block: NestedNodeReader<T>): T =
3130
with(reader) {
3231
beginObject()
3332
val item = mk()
3433
while (hasNext()) {
35-
block.accept(item, nextName())
34+
block.read(item, nextName())
3635
}
3736
endObject()
3837
item
3938
}
4039

41-
fun <T> listR(mk: () -> T, reader: JsonReader, block: BiConsumer<T, String>): List<T?>? =
40+
fun <T> listR(mk: () -> T, reader: JsonReader, fn: NestedNodeReader<T>): List<T?>? =
4241
reader.skipNullOr {
4342
val items = mutableListOf<T?>()
4443
beginArray()
45-
while (hasNext()) items += objR(mk, this, block)
44+
while (hasNext()) items += objR(mk, this, fn)
4645
endArray()
4746
items
4847
}
@@ -61,6 +60,8 @@ private fun <T> JsonReader.skipNullOr(fn: JsonReader.() -> T): T? =
6160
fun <T> JsonReader.readProps(pojoType: Class<T>, bean: T, fieldName: String, moshi: Moshi) =
6261
skipNullOr {
6362
val propType: Class<*> = BeanUtils.findPropertyType(fieldName, pojoType)
63+
// * NOTE 15 Feb 2024 gopala.akshintala: Since data type info is lost with JSON, we cannot use
64+
// dynamicAdapter
6465
val delegate: JsonAdapter<out Any?> = moshi.adapter(propType)
6566
BeanUtils.getPropertyDescriptor(pojoType, fieldName)
6667
?.writeMethod

src/main/kotlin/com/salesforce/revoman/input/json/JsonWriterUtils.kt

Lines changed: 24 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -11,32 +11,26 @@ package com.salesforce.revoman.input.json
1111

1212
import com.squareup.moshi.JsonAdapter
1313
import com.squareup.moshi.JsonWriter
14-
import com.squareup.moshi.Moshi
15-
import java.util.function.Consumer
1614
import org.springframework.beans.BeanUtils
1715

18-
fun <T> objW(name: String, obj: T, writer: JsonWriter, block: Consumer<T>): Unit =
16+
fun <T> objW(name: String, obj: T, writer: JsonWriter, fn: NestedNodeWriter<T>): Unit =
1917
with(writer) {
2018
name(name)
21-
objW(obj, writer, block)
19+
objW(obj, writer, fn)
2220
}
2321

24-
fun <T> objW(obj: T, writer: JsonWriter, block: Consumer<T>): Unit =
22+
fun <T> objW(obj: T, writer: JsonWriter, fn: NestedNodeWriter<T>): Unit =
2523
with(writer) {
2624
if (obj == null) {
2725
nullValue()
2826
} else {
2927
beginObject()
30-
block.accept(obj)
28+
fn.write(obj)
3129
endObject()
3230
}
3331
}
3432

35-
fun string(name: String, value: String?, writer: JsonWriter): Unit =
36-
with(writer) {
37-
name(name)
38-
value?.also(::value) ?: nullValue()
39-
}
33+
fun string(name: String, value: String?, writer: JsonWriter): Unit = writer.string(name, value)
4034

4135
fun JsonWriter.string(name: String, value: String?) {
4236
name(name)
@@ -54,64 +48,62 @@ fun JsonWriter.bool(name: String, value: Boolean?) {
5448
value?.also(::value) ?: nullValue()
5549
}
5650

57-
fun integer(name: String, value: Int?, writer: JsonWriter): Unit =
58-
with(writer) {
59-
name(name)
60-
value?.also(::value) ?: nullValue()
61-
}
51+
fun integer(name: String, value: Int?, writer: JsonWriter): Unit = writer.integer(name, value)
6252

6353
fun JsonWriter.integer(name: String, value: Int?) {
6454
name(name)
6555
value?.also(::value) ?: nullValue()
6656
}
6757

68-
fun doubl(name: String, value: Double?, writer: JsonWriter): Unit =
69-
with(writer) {
70-
name(name)
71-
value?.also(::value) ?: nullValue()
72-
}
58+
fun doubl(name: String, value: Double?, writer: JsonWriter): Unit = writer.doubl(name, value)
7359

7460
fun JsonWriter.doubl(name: String, value: Double?) {
7561
name(name)
7662
value?.also(::value) ?: nullValue()
7763
}
7864

79-
fun lng(name: String, value: Long?, writer: JsonWriter): Unit =
80-
with(writer) {
81-
name(name)
82-
value?.also(::value) ?: nullValue()
83-
}
65+
fun lng(name: String, value: Long?, writer: JsonWriter): Unit = writer.lng(name, value)
8466

8567
fun JsonWriter.lng(name: String, value: Long?) {
8668
name(name)
8769
value?.also(::value) ?: nullValue()
8870
}
8971

90-
fun <T> listW(name: String, list: List<T>?, writer: JsonWriter, block: Consumer<T>): JsonWriter =
72+
fun <T> listW(
73+
name: String,
74+
list: List<T>?,
75+
writer: JsonWriter,
76+
block: NestedNodeWriter<T>
77+
): JsonWriter =
9178
with(writer) {
9279
name(name)
9380
listW(list, this, block)
9481
}
9582

96-
fun <T> listW(list: List<T>?, writer: JsonWriter, block: Consumer<T>): JsonWriter =
83+
fun <T> listW(list: List<T>?, writer: JsonWriter, fn: NestedNodeWriter<T>): JsonWriter =
9784
with(writer) {
9885
when (list) {
9986
null -> nullValue()
10087
else -> {
10188
beginArray()
102-
list.forEach(block)
89+
list.forEach(fn::write)
10390
endArray()
10491
}
10592
}
10693
}
10794

108-
// ! TODO 18/10/23 gopala.akshintala: Abstract it into a common artifact to be used in ReVoman
95+
fun JsonWriter.mapW(map: Map<String, Any?>, dynamicJsonAdapter: JsonAdapter<Any>) {
96+
map.forEach { (key: String, value: Any?) ->
97+
name(key)
98+
dynamicJsonAdapter.toJson(this, value)
99+
}
100+
}
109101

110102
fun <T> JsonWriter.writeProps(
111103
pojoType: Class<T>,
112104
bean: T,
113105
excludePropTypes: Set<Class<*>>,
114-
moshi: Moshi
106+
dynamicJsonAdapter: JsonAdapter<Any>,
115107
) =
116108
BeanUtils.getPropertyDescriptors(pojoType)
117109
.asSequence()
@@ -123,6 +115,5 @@ fun <T> JsonWriter.writeProps(
123115
throw IllegalStateException("Please add a getter for the Property: `${it.name}`")
124116
}
125117
name(it.name)
126-
val delegate = moshi.adapter(it.propertyType) as JsonAdapter<Any>
127-
delegate.toJson(this, it.readMethod(bean))
118+
dynamicJsonAdapter.toJson(this, it.readMethod(bean))
128119
}

src/test/java/com/salesforce/revoman/input/JsonPojoUtilsTest.java renamed to src/test/java/com/salesforce/revoman/input/json/JsonPojoUtilsTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@
55
* http://www.apache.org/licenses/LICENSE-2.0
66
**************************************************************************************************/
77

8-
package com.salesforce.revoman.input;
8+
package com.salesforce.revoman.input.json;
99

1010
import static com.google.common.truth.Truth.assertThat;
1111

12-
import com.salesforce.revoman.input.json.JsonPojoUtils;
1312
import java.util.Date;
1413
import java.util.List;
1514
import org.junit.jupiter.api.DisplayName;
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2023 salesforce.com, inc.
3+
* All Rights Reserved
4+
* Company Confidential
5+
*/
6+
7+
package com.salesforce.revoman.input.json;
8+
9+
import static com.salesforce.revoman.input.FileUtils.readFileInResourcesToString;
10+
11+
import com.salesforce.revoman.input.json.adapters.SObjectGraphRequestMarshaller;
12+
import com.salesforce.revoman.input.json.pojo.Entity;
13+
import com.salesforce.revoman.input.json.pojo.SObjectGraphRequest;
14+
import com.salesforce.revoman.input.json.pojo.SObjectWithReferenceRequest;
15+
import java.util.List;
16+
import java.util.Map;
17+
import org.json.JSONException;
18+
import org.junit.jupiter.api.DisplayName;
19+
import org.junit.jupiter.api.Test;
20+
import org.skyscreamer.jsonassert.JSONAssert;
21+
import org.skyscreamer.jsonassert.JSONCompareMode;
22+
23+
class SObjectGraphRequestMarshallerTest {
24+
25+
@DisplayName("toJson: SObjectGraphRequest POJO --> PQ Payload JSON")
26+
@Test
27+
void sObjectGraphMarshallToPQPayload() throws JSONException {
28+
final var pqTestInputRepMarshaller =
29+
SObjectGraphRequestMarshaller.adapter(
30+
Map.of("pricingPref", "skip", "configurationInput", "skip"));
31+
final var pqPayloadJsonStr =
32+
JsonPojoUtils.pojoToJson(
33+
SObjectGraphRequest.class,
34+
prepareSObjectGraphReqPojo(),
35+
List.of(pqTestInputRepMarshaller));
36+
final var expectedPQPayload = readFileInResourcesToString("json/pq-graph-req.json");
37+
JSONAssert.assertEquals(expectedPQPayload, pqPayloadJsonStr, JSONCompareMode.STRICT);
38+
}
39+
40+
static SObjectGraphRequest prepareSObjectGraphReqPojo() {
41+
return new SObjectGraphRequest(
42+
"pq-update-quote",
43+
List.of(
44+
new SObjectWithReferenceRequest(
45+
"refQuote",
46+
new Entity(
47+
Map.of(
48+
"attributes",
49+
Map.of("type", "Quote", "method", "PATCH", "id", "quoteId"),
50+
"Name",
51+
"Overfullstack")))));
52+
}
53+
}

0 commit comments

Comments
 (0)