-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1470 from disneystreaming/smithy4s-schema-to-smithy
Add functionality to recreate a smithy model from smithy4s schemas/services
- Loading branch information
Showing
13 changed files
with
1,086 additions
and
19 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
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
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,45 @@ | ||
--- | ||
sidebar_label: Smithy4s to Smithy | ||
title: Converting Smithy4s Schemas and Services to Smithy | ||
--- | ||
|
||
Using the smithy4s dynamic module you can convert a smithy4s service or schema into a smithy model. This guide will walk through the steps to do this. | ||
|
||
## Creating a DynamicSchemaIndex | ||
|
||
The first step is to take the services and schemas you'd like included in your smithy model and add them to a DynamicSchemaIndex using the provided builder. | ||
|
||
```scala mdoc | ||
import smithy4s.dynamic.DynamicSchemaIndex | ||
|
||
val dynamicSchemaIndex = DynamicSchemaIndex.builder | ||
.addService[smithy4s.example.KVStoreGen] | ||
.addSchema[smithy4s.example.FaceCard] | ||
.build() | ||
``` | ||
|
||
## Converting to Smithy Model | ||
|
||
Now that we have a DynamicSchemaIndex, we can convert to a smithy model object from the smithy-model Java library. This feature is only supported on the JVM and not ScalaJS or Scala Native. | ||
|
||
```scala mdoc | ||
val model = dynamicSchemaIndex.toSmithyModel | ||
``` | ||
|
||
## Rendering as a String | ||
|
||
If you wish to render the smithy `Model` as a String, smithy-model provides a method to accomplish this. | ||
|
||
```scala mdoc | ||
import software.amazon.smithy.model.shapes.SmithyIdlModelSerializer | ||
import scala.jdk.CollectionConverters._ | ||
import java.nio.file.Path | ||
|
||
val smithyFiles: Map[Path, String] = | ||
SmithyIdlModelSerializer | ||
.builder() | ||
.build() | ||
.serialize(model) | ||
.asScala | ||
.toMap | ||
``` |
19 changes: 19 additions & 0 deletions
19
modules/dynamic/src-js-native/DynamicSchemaIndexCompanionPlatform.scala
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,19 @@ | ||
/* | ||
* Copyright 2021-2024 Disney Streaming | ||
* | ||
* Licensed under the Tomorrow Open Source Technology License, Version 1.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://disneystreaming.github.io/TOST-1.0.txt | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package smithy4s.dynamic | ||
|
||
private[dynamic] trait DynamicSchemaIndexCompanionPlatform {} |
43 changes: 43 additions & 0 deletions
43
modules/dynamic/src-jvm/DynamicSchemaIndexCompanionPlatform.scala
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,43 @@ | ||
/* | ||
* Copyright 2021-2024 Disney Streaming | ||
* | ||
* Licensed under the Tomorrow Open Source Technology License, Version 1.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://disneystreaming.github.io/TOST-1.0.txt | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package smithy4s.dynamic | ||
|
||
import software.amazon.smithy.model.shapes.ModelSerializer | ||
import software.amazon.smithy.model.transform.ModelTransformer | ||
|
||
private[dynamic] trait DynamicSchemaIndexCompanionPlatform { | ||
self: DynamicSchemaIndex.type => | ||
|
||
/** | ||
* Loads a dynamic schema index model from a smithy model. | ||
*/ | ||
def loadModel( | ||
model: software.amazon.smithy.model.Model | ||
): DynamicSchemaIndex = { | ||
val flattenedModel = | ||
ModelTransformer.create().flattenAndRemoveMixins(model); | ||
val node = ModelSerializer.builder().build.serialize(flattenedModel) | ||
val document = NodeToDocument(node) | ||
smithy4s.Document | ||
.decode[smithy4s.dynamic.model.Model](document) | ||
.map(load(_)) match { | ||
case Left(error) => throw error | ||
case Right(value) => value | ||
} | ||
} | ||
|
||
} |
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
Oops, something went wrong.