Skip to content

Commit

Permalink
Merge pull request #1087 from disneystreaming/namespace-keywords
Browse files Browse the repository at this point in the history
escape keywords in namespaces
  • Loading branch information
Baccata authored Jul 17, 2023
2 parents d7d2c05 + 5c04db8 commit c1f0883
Show file tree
Hide file tree
Showing 10 changed files with 78 additions and 5 deletions.
1 change: 1 addition & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -821,6 +821,7 @@ lazy val bootstrapped = projectMatrix
"smithy4s.example.guides.hello",
"smithy4s.example.hello",
"smithy4s.example.test",
"smithy4s.example.package",
"weather",
"smithy4s.example.product"
),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package smithy4s.example._package

import smithy4s.Hints
import smithy4s.Newtype
import smithy4s.Schema
import smithy4s.ShapeId
import smithy4s.schema.Schema.bijection
import smithy4s.schema.Schema.string

object MyPackageString extends Newtype[String] {
val id: ShapeId = ShapeId("smithy4s.example.package", "MyPackageString")
val hints: Hints = Hints.empty
val underlyingSchema: Schema[String] = string.withId(id).addHints(hints)
implicit val schema: Schema[MyPackageString] = bijection(underlyingSchema, asBijection)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package smithy4s.example

package object _package {

type MyPackageString = smithy4s.example._package.MyPackageString.Type

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package smithy4s.example.collision

import smithy4s.Hints
import smithy4s.Schema
import smithy4s.ShapeId
import smithy4s.ShapeTag
import smithy4s.example._package.MyPackageString
import smithy4s.schema.Schema.struct

final case class TestReservedNamespaceImport(_package: Option[MyPackageString] = None)
object TestReservedNamespaceImport extends ShapeTag.Companion[TestReservedNamespaceImport] {
val id: ShapeId = ShapeId("smithy4s.example.collision", "TestReservedNamespaceImport")

val hints: Hints = Hints.empty

implicit val schema: Schema[TestReservedNamespaceImport] = struct(
MyPackageString.schema.optional[TestReservedNamespaceImport]("package", _._package),
){
TestReservedNamespaceImport.apply
}.withId(id).addHints(hints)
}
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ private[internals] object CollisionAvoidance {
if (reservedKeywords(str)) s"_$str" else str

private val names = new Names()
private def protectType(str: String): String =
private[internals] def protectType(str: String): String =
if (names.getReservedNames(str)) "_" + str else protectKeyword(str)

private val reservedKeywords: Set[String] = Set(
Expand Down
7 changes: 5 additions & 2 deletions modules/codegen/src/smithy4s/codegen/internals/IR.scala
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,13 @@ import UnionMember._
import LineSegment.{NameDef, NameRef}

private[internals] case class CompilationUnit(
namespace: String,
rawNamespace: String,
declarations: List[Decl],
rendererConfig: Renderer.Config
)
) {
val namespace: String =
rawNamespace.split('.').map(CollisionAvoidance.protectType(_)).mkString(".")
}

private[internals] sealed trait Decl {
def shapeId: ShapeId
Expand Down
10 changes: 8 additions & 2 deletions modules/codegen/src/smithy4s/codegen/internals/LineSegment.scala
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,15 @@ private[codegen] object LineSegment {
implicit val nameDefShow: Show[NameDef] = Show.show[NameDef](_.name)
}
// A Reference to a Scala type or value s.
case class NameRef(pkg: List[String], name: String, typeParams: List[NameRef])
extends LineSegment {
case class NameRef(
rawPkg: List[String],
name: String,
typeParams: List[NameRef]
) extends LineSegment {
self =>

def pkg: List[String] = rawPkg.map(CollisionAvoidance.protectType(_))

def asValue: String = s"${(pkg :+ name).mkString(".")}"

def asImport: String = s"${(pkg :+ getNamePrefix).mkString(".")}"
Expand Down
10 changes: 10 additions & 0 deletions modules/website/src/theme/SearchBar/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import React from 'react';
import SearchBar from '@theme-original/SearchBar';

export default function SearchBarWrapper(props) {
return (
<>
<SearchBar {...props} />
</>
);
}
5 changes: 5 additions & 0 deletions sampleSpecs/reservedNamespace.smithy
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
$version: "2.0"

namespace smithy4s.example.package

string MyPackageString
5 changes: 5 additions & 0 deletions sampleSpecs/reservednames.smithy
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ $version: "2.0"
namespace smithy4s.example.collision

use alloy#simpleRestJson
use smithy4s.example.package#MyPackageString

@simpleRestJson
service ReservedNameService {
Expand Down Expand Up @@ -56,3 +57,7 @@ operation Option {
}

string String

structure TestReservedNamespaceImport {
package: MyPackageString
}

0 comments on commit c1f0883

Please sign in to comment.