-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
recursively merge package.json from build configuration
- Loading branch information
Showing
3 changed files
with
151 additions
and
3 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
42 changes: 42 additions & 0 deletions
42
...-codegen/src/main/java/software/amazon/smithy/typescript/codegen/util/MergeJsonNodes.java
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,42 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package software.amazon.smithy.typescript.codegen.util; | ||
|
||
import java.util.Objects; | ||
import software.amazon.smithy.model.node.ObjectNode; | ||
|
||
|
||
public final class MergeJsonNodes { | ||
|
||
private MergeJsonNodes() {} | ||
|
||
/** | ||
* @param left - original node. | ||
* @param right - overwriting node. | ||
* @return new node with shallow merged fields except the recursively merged "scripts" field. | ||
*/ | ||
public static ObjectNode mergeWithScripts(ObjectNode left, ObjectNode right) { | ||
Objects.requireNonNull(left); | ||
Objects.requireNonNull(right); | ||
|
||
ObjectNode.Builder merged = left.toBuilder(); | ||
right.getMembers().forEach((k, v) -> { | ||
String key = k.getValue(); | ||
if (left.containsMember(key)) { | ||
if (left.getMember(key).get().isObjectNode() && v.isObjectNode() && key.equals("scripts")) { | ||
merged.withMember(key, | ||
MergeJsonNodes.mergeWithScripts(left.expectObjectMember(key), v.expectObjectNode()) | ||
); | ||
} else { | ||
merged.withMember(key, v); | ||
} | ||
} else { | ||
merged.withMember(key, v); | ||
} | ||
}); | ||
return merged.build(); | ||
} | ||
} |
103 changes: 103 additions & 0 deletions
103
...egen/src/test/java/software/amazon/smithy/typescript/codegen/util/MergeJsonNodesTest.java
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,103 @@ | ||
package software.amazon.smithy.typescript.codegen.util; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import software.amazon.smithy.model.node.Node; | ||
import software.amazon.smithy.model.node.ObjectNode; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
class MergeJsonNodesTest { | ||
|
||
@Test | ||
void apply() { | ||
ObjectNode left = ObjectNode.parse(""" | ||
{ | ||
"name": "hello, world", | ||
"version": 5, | ||
"scripts": { | ||
"target": "exec", | ||
"target2": "exec2", | ||
"args": ["a", "b", "c"], | ||
"nested": { | ||
"deep": "." | ||
} | ||
}, | ||
"metadata": { | ||
"A": "A", | ||
"B": "B", | ||
"C": "C" | ||
} | ||
} | ||
""").expectObjectNode(); | ||
|
||
ObjectNode right = ObjectNode.parse(""" | ||
{ | ||
"version": 6, | ||
"scripts": { | ||
"target": "no-op", | ||
"args": ["a", "b", "c", "d"], | ||
"nested": { | ||
"option": "b" | ||
} | ||
}, | ||
"metadata": { | ||
"A": "A" | ||
} | ||
} | ||
""").expectObjectNode(); | ||
|
||
ObjectNode expected = ObjectNode.parse(""" | ||
{ | ||
"name": "hello, world", | ||
"version": 6, | ||
"scripts": { | ||
"target": "no-op", | ||
"target2": "exec2", | ||
"args": ["a", "b", "c", "d"], | ||
"nested": { | ||
"option": "b" | ||
} | ||
}, | ||
"metadata": { | ||
"A": "A" | ||
} | ||
} | ||
""").expectObjectNode(); | ||
|
||
ObjectNode l = expected; | ||
ObjectNode r = MergeJsonNodes.mergeWithScripts(left, right); | ||
|
||
assertEquals( | ||
l.expectStringMember("name"), | ||
r.expectStringMember("name") | ||
); | ||
assertEquals( | ||
l.expectNumberMember("version"), | ||
r.expectNumberMember("version") | ||
); | ||
assertEquals( | ||
l.expectObjectMember("scripts").expectStringMember("target"), | ||
r.expectObjectMember("scripts").expectStringMember("target") | ||
); | ||
assertEquals( | ||
l.expectObjectMember("scripts").expectStringMember("target2"), | ||
r.expectObjectMember("scripts").expectStringMember("target2") | ||
); | ||
assertEquals( | ||
l.expectObjectMember("scripts").expectArrayMember("args").getElementsAs(Node::toString), | ||
r.expectObjectMember("scripts").expectArrayMember("args").getElementsAs(Node::toString) | ||
); | ||
assertEquals( | ||
l.expectObjectMember("scripts").expectObjectMember("nested").expectStringMember("option"), | ||
r.expectObjectMember("scripts").expectObjectMember("nested").expectStringMember("option") | ||
); | ||
assertEquals( | ||
1, | ||
r.expectObjectMember("scripts").expectObjectMember("nested").getStringMap().size() | ||
); | ||
assertEquals( | ||
1, | ||
r.expectObjectMember("metadata").getStringMap().size() | ||
); | ||
} | ||
} |