Skip to content

Commit

Permalink
Update the proto translation to use the protobuf's "well know types"
Browse files Browse the repository at this point in the history
  • Loading branch information
Baccata committed Jan 26, 2024
1 parent 4f5c339 commit 57617ae
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 62 deletions.
7 changes: 2 additions & 5 deletions modules/proto/src/smithyproto/proto3/Compiler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,13 @@ import software.amazon.smithy.model.Model
import software.amazon.smithy.model.shapes._
import software.amazon.smithy.model.traits.DeprecatedTrait
import software.amazon.smithy.model.traits.EnumTrait
import software.amazon.smithy.model.traits.RequiredTrait
import software.amazon.smithy.model.traits.UnitTypeTrait
import software.amazon.smithy.model.traits.TraitDefinition

import scala.jdk.CollectionConverters._
import scala.jdk.OptionConverters._
import scala.annotation.nowarn
import software.amazon.smithy.model.neighbor.NeighborProvider
import software.amazon.smithy.model.neighbor.Walker
import smithytranslate.closure.IdRefVisitor

class Compiler(model: Model, allShapes: Boolean) {

Expand Down Expand Up @@ -482,7 +479,7 @@ class Compiler(model: Model, allShapes: Boolean) {
else Type.AlloyWrappers.ByteValue
}
def documentShape(shape: DocumentShape): Option[Type] = Some {
if (!isWrapped) Type.AlloyTypes.Document
if (!isWrapped) Type.GoogleValue
else Type.AlloyWrappers.Document
}

Expand Down Expand Up @@ -556,7 +553,7 @@ class Compiler(model: Model, allShapes: Boolean) {
}

def timestampShape(shape: TimestampShape): Option[Type] = Some {
if (!isWrapped) Type.AlloyTypes.Timestamp
if (!isWrapped) Type.GoogleTimestamp
else Type.AlloyWrappers.Timestamp
}

Expand Down
25 changes: 15 additions & 10 deletions modules/proto/src/smithyproto/proto3/ProtoIR.scala
Original file line number Diff line number Diff line change
Expand Up @@ -153,18 +153,10 @@ object ProtoIR {
Fqn(Some(List("alloy", "protobuf")), "wrappers")

object AlloyTypes {
val Timestamp = RefType(
alloyFqn("Timestamp"),
alloyTypesImport
)
val CompactUUID = RefType(
alloyFqn("CompactUUID"),
alloyTypesImport
)
val Document = RefType(
alloyFqn("Document"),
alloyTypesImport
)
}

object AlloyWrappers {
Expand Down Expand Up @@ -224,10 +216,23 @@ object ProtoIR {

// https://github.com/protocolbuffers/protobuf/blob/178ebc179ede26bcaa85b39db127ebf099be3ef8/src/google/protobuf/wrappers.proto

trait GoogleWrappers extends Type {
def importFqn = Set(protobufFqn("wrappers"))
sealed trait PredefinedType extends Type {
def fqn: Fqn
}
sealed trait GoogleWrappers extends PredefinedType {
def importFqn = Set(protobufFqn("wrappers"))
}

case object GoogleValue extends PredefinedType {
def importFqn: Set[Fqn] = Set(protobufFqn("struct"))
def fqn = protobufFqn("Value")
}

case object GoogleTimestamp extends PredefinedType {
def importFqn: Set[Fqn] = Set(protobufFqn("timestamp"))
def fqn = protobufFqn("Timestamp")
}

object GoogleWrappers {
case object Double extends GoogleWrappers {
def fqn: Fqn = protobufFqn("DoubleValue")
Expand Down
2 changes: 1 addition & 1 deletion modules/proto/src/smithyproto/proto3/Renderer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ object Renderer {
case RefType(fqn, _) => fqn.render
case Any => Any.fqn.render
case Empty => Empty.fqn.render
case w: GoogleWrappers => w.fqn.render
case w: PredefinedType => w.fqn.render
}
}

Expand Down
28 changes: 17 additions & 11 deletions modules/proto/tests/src/CompilerRendererSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,10 @@ class CompilerRendererSuite extends FunSuite {
|
|package com.example;
|
|import "alloy/protobuf/types.proto";
|import "google/protobuf/struct.proto";
|
|message SomeDoc {
| alloy.protobuf.Document value = 1;
| google.protobuf.Value value = 1;
|}
|""".stripMargin

Expand Down Expand Up @@ -179,7 +179,9 @@ class CompilerRendererSuite extends FunSuite {
|
|package com.example;
|
|import "alloy/protobuf/types.proto";
|import "google/protobuf/struct.proto";
|
|import "google/protobuf/timestamp.proto";
|
|message Struct {
| bool boolean = 1;
Expand All @@ -192,9 +194,9 @@ class CompilerRendererSuite extends FunSuite {
| string bigInteger = 8;
| string bigDecimal = 9;
| bytes blob = 10;
| alloy.protobuf.Document document = 11;
| google.protobuf.Value document = 11;
| string string = 12;
| alloy.protobuf.Timestamp timestamp = 13;
| google.protobuf.Timestamp timestamp = 13;
|}
|""".stripMargin
convertCheck(source, Map("com/example/example.proto" -> expected))
Expand Down Expand Up @@ -302,7 +304,9 @@ class CompilerRendererSuite extends FunSuite {
|
|package com.example;
|
|import "alloy/protobuf/types.proto";
|import "google/protobuf/struct.proto";
|
|import "google/protobuf/timestamp.proto";
|
|message Struct {
| bool boolean = 1;
Expand All @@ -315,9 +319,9 @@ class CompilerRendererSuite extends FunSuite {
| string bigInteger = 8;
| string bigDecimal = 9;
| bytes blob = 10;
| alloy.protobuf.Document document = 11;
| google.protobuf.Value document = 11;
| string string = 12;
| alloy.protobuf.Timestamp timestamp = 13;
| google.protobuf.Timestamp timestamp = 13;
|}
|""".stripMargin
convertCheck(source, Map("com/example/example.proto" -> expected))
Expand Down Expand Up @@ -376,7 +380,9 @@ class CompilerRendererSuite extends FunSuite {
|
|package com.example;
|
|import "alloy/protobuf/types.proto";
|import "google/protobuf/struct.proto";
|
|import "google/protobuf/timestamp.proto";
|
|message MyBoolean {
| bool value = 1;
Expand Down Expand Up @@ -419,15 +425,15 @@ class CompilerRendererSuite extends FunSuite {
|}
|
|message MyDocument {
| alloy.protobuf.Document value = 1;
| google.protobuf.Value value = 1;
|}
|
|message MyString {
| string value = 1;
|}
|
|message MyTimestamp {
| alloy.protobuf.Timestamp value = 1;
| google.protobuf.Timestamp value = 1;
|}
|
|message Struct {
Expand Down
1 change: 0 additions & 1 deletion modules/proto/tests/src/CompilerSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import smithyproto.proto3.ProtoIR._
import smithyproto.proto3.ProtoIR.Statement._
import software.amazon.smithy.model.Model
import software.amazon.smithy.model.shapes.StringShape
import alloy.proto.ProtoWrappedTrait
import software.amazon.smithy.model.shapes.StructureShape
import alloy.proto.ProtoEnabledTrait
import software.amazon.smithy.model.shapes.ShapeId
Expand Down
35 changes: 11 additions & 24 deletions modules/proto/tests/src/ProtocInvocationHelper.scala
Original file line number Diff line number Diff line change
Expand Up @@ -34,36 +34,23 @@ import scala.io.Source
trait ProtocInvocationHelper {
private lazy val protoc = ProtocRunner.forVersion(Version.protobufVersion)

private def loadProtoFiles(paths: String*): List[(String, String)] = {
paths.map { p =>
val file = new File(getClass.getResource(p).getPath())
p -> Source
.fromFile(file)
.getLines()
.mkString("\n")
private def loadProtoFiles(directories: String*): List[(String, String)] = {
directories.flatMap { d =>
val dir = new File(getClass.getResource(d).getPath())
dir.listFiles().toSeq.filter(_.getName().endsWith(".proto")).map { file =>
(d + "/" + file.getName) -> Source
.fromFile(file)
.getLines()
.mkString("\n")
}
}.toList
// val dir = "/google/protobuf/"
// val path = getClass.getResource(dir)
// val folder = new File(path.getPath)
// if (folder.exists && folder.isDirectory) {
// folder.listFiles.toList
// .collect {
// case file if paths.contains(file.getName) =>
// dir + file.getName -> Source
// .fromFile(file)
// .getLines()
// .mkString("\n")
// }
// } else List.empty
}

def generateFileSet(files: Seq[(String, String)]): Seq[FileDescriptor] = {
val tmpDir = Files.createTempDirectory("validation").toFile
val extraFiles = loadProtoFiles(
"/google/protobuf/wrappers.proto",
"/google/protobuf/any.proto",
"/alloy/protobuf/types.proto",
"/alloy/protobuf/wrappers.proto"
"/google/protobuf/",
"/alloy/protobuf/"
)
val allFiles = files ++ extraFiles
val fileNames = allFiles.map { case (name, content) =>
Expand Down
16 changes: 6 additions & 10 deletions modules/readme-validator/src/Validator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import cats.data.NonEmptyList
import java.nio.file.Path
import scala.jdk.CollectionConverters._
import scala.util.control.NoStackTrace
import smithyproto.proto3.{Compiler => ProtoCompiler, ModelPreProcessor}
import smithyproto.proto3.{Compiler => ProtoCompiler}
import smithytranslate.compiler._
import smithytranslate.compiler.openapi._
import smithytranslate.compiler.json_schema._
Expand Down Expand Up @@ -191,10 +191,10 @@ object Validator {
val ns = "foo"
ns ->
s"""|$$version: "2"
|
|namespace $ns
|
|$smithy""".stripMargin
|
|namespace $ns
|
|$smithy""".stripMargin
}
val getActualProto: String => String = { proto =>
val lines = proto.split("\n")
Expand All @@ -215,12 +215,8 @@ object Validator {
.addUnparsedModel(s"$namespace.smithy", actualSmithy)
.assemble()
.unwrap()
val preprocessedModel = ModelPreProcessor(
inputModel,
List(ModelPreProcessor.transformers.PreventEnumConflicts)
)

val compiler = new ProtoCompiler(preprocessedModel)
val compiler = new ProtoCompiler(inputModel, allShapes = false)
val result = compiler.compile()

val rendered = result
Expand Down

0 comments on commit 57617ae

Please sign in to comment.