Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optics Rendering #1103

Merged
merged 19 commits into from
Jul 27, 2023
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ typeclasses such as `cats.Show`, `cats.Eq` or any others you may be using.

See https://github.com/disneystreaming/smithy4s/pull/912

### Smithy4s Optics Instances

When the smithy4sRenderOptics setting is enabled, Lenses and Prisms will be rendered in the companion objects of the generated code when appropriate.

See <!-- TODO Put link once PR is up -->

# 0.17.5

This release is backward binary-compatible with the previous releases from the 0.17.x lineage.
Expand Down
3 changes: 2 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,8 @@ lazy val docs =
Dependencies.Http4s.emberClient.value,
Dependencies.Http4s.emberServer.value,
Dependencies.Decline.effect.value,
Dependencies.AwsSpecSummary.value
Dependencies.AwsSpecSummary.value,
Dependencies.Monocle.core.value
)
)
.settings(Smithy4sBuildPlugin.doNotPublishArtifact)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import smithy4s.Hints
import smithy4s.Schema
import smithy4s.ShapeId
import smithy4s.ShapeTag
import smithy4s.optics.Lens
import smithy4s.schema.Schema.struct

/** @param Endpoints
Expand All @@ -15,6 +16,10 @@ object DescribeEndpointsResponse extends ShapeTag.Companion[DescribeEndpointsRes

val hints: Hints = Hints.empty

object Optics {
val endpoints = Lens[DescribeEndpointsResponse, List[Endpoint]](_.endpoints)(n => a => a.copy(endpoints = n))
}

implicit val schema: Schema[DescribeEndpointsResponse] = struct(
Endpoints.underlyingSchema.required[DescribeEndpointsResponse]("Endpoints", _.endpoints).addHints(smithy.api.Documentation("<p>List of endpoints.</p>"), smithy.api.Required()),
){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import smithy4s.StreamingSchema
import smithy4s.Transformation
import smithy4s.kinds.PolyFunction5
import smithy4s.kinds.toPolyFunction5.const5
import smithy4s.optics.Prism
import smithy4s.schema.Schema.UnionSchema
import smithy4s.schema.Schema.bijection
import smithy4s.schema.Schema.union
Expand Down Expand Up @@ -164,6 +165,11 @@ object DynamoDBOperation {

val hints: Hints = Hints.empty

object Optics {
val InternalServerError = Prism.partial[ListTablesError, InternalServerError]{ case InternalServerErrorCase(t) => t }(InternalServerErrorCase.apply)
val InvalidEndpointException = Prism.partial[ListTablesError, InvalidEndpointException]{ case InvalidEndpointExceptionCase(t) => t }(InvalidEndpointExceptionCase.apply)
}

final case class InternalServerErrorCase(internalServerError: InternalServerError) extends ListTablesError
final case class InvalidEndpointExceptionCase(invalidEndpointException: InvalidEndpointException) extends ListTablesError

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import smithy4s.Hints
import smithy4s.Schema
import smithy4s.ShapeId
import smithy4s.ShapeTag
import smithy4s.optics.Lens
import smithy4s.schema.Schema.long
import smithy4s.schema.Schema.string
import smithy4s.schema.Schema.struct
Expand All @@ -22,6 +23,11 @@ object Endpoint extends ShapeTag.Companion[Endpoint] {
smithy.api.Documentation("<p>An endpoint information details.</p>"),
)

object Optics {
val address = Lens[Endpoint, String](_.address)(n => a => a.copy(address = n))
val cachePeriodInMinutes = Lens[Endpoint, Long](_.cachePeriodInMinutes)(n => a => a.copy(cachePeriodInMinutes = n))
}

implicit val schema: Schema[Endpoint] = struct(
string.required[Endpoint]("Address", _.address).addHints(smithy.api.Documentation("<p>IP address of the endpoint.</p>"), smithy.api.Required()),
long.required[Endpoint]("CachePeriodInMinutes", _.cachePeriodInMinutes).addHints(smithy.api.Default(smithy4s.Document.fromDouble(0.0d)), smithy.api.Required(), smithy.api.Documentation("<p>Endpoint cache time to live (TTL) value.</p>")),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import smithy4s.Hints
import smithy4s.Schema
import smithy4s.ShapeId
import smithy4s.ShapeTag
import smithy4s.optics.Lens
import smithy4s.schema.Schema.struct

/** <p>An error occurred on the server side.</p>
Expand All @@ -21,6 +22,10 @@ object InternalServerError extends ShapeTag.Companion[InternalServerError] {
smithy.api.Error.SERVER.widen,
)

object Optics {
val message = Lens[InternalServerError, Option[ErrorMessage]](_.message)(n => a => a.copy(message = n))
}

implicit val schema: Schema[InternalServerError] = struct(
ErrorMessage.schema.optional[InternalServerError]("message", _.message).addHints(smithy.api.Documentation("<p>The server encountered an internal error trying to fulfill the request.</p>")),
){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import smithy4s.Hints
import smithy4s.Schema
import smithy4s.ShapeId
import smithy4s.ShapeTag
import smithy4s.optics.Lens
import smithy4s.schema.Schema.string
import smithy4s.schema.Schema.struct

Expand All @@ -18,6 +19,10 @@ object InvalidEndpointException extends ShapeTag.Companion[InvalidEndpointExcept
smithy.api.HttpError(421),
)

object Optics {
val message = Lens[InvalidEndpointException, Option[String]](_.message)(n => a => a.copy(message = n))
}

implicit val schema: Schema[InvalidEndpointException] = struct(
string.optional[InvalidEndpointException]("Message", _.message),
){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import smithy4s.Hints
import smithy4s.Schema
import smithy4s.ShapeId
import smithy4s.ShapeTag
import smithy4s.optics.Lens
import smithy4s.schema.Schema.struct

/** <p>Represents the input of a <code>ListTables</code> operation.</p>
Expand All @@ -22,6 +23,11 @@ object ListTablesInput extends ShapeTag.Companion[ListTablesInput] {
smithy.api.Documentation("<p>Represents the input of a <code>ListTables</code> operation.</p>"),
)

object Optics {
val exclusiveStartTableName = Lens[ListTablesInput, Option[TableName]](_.exclusiveStartTableName)(n => a => a.copy(exclusiveStartTableName = n))
val limit = Lens[ListTablesInput, Option[ListTablesInputLimit]](_.limit)(n => a => a.copy(limit = n))
}

implicit val schema: Schema[ListTablesInput] = struct(
TableName.schema.optional[ListTablesInput]("ExclusiveStartTableName", _.exclusiveStartTableName).addHints(smithy.api.Documentation("<p>The first table name that this operation will evaluate. Use the value that was returned for\n <code>LastEvaluatedTableName</code> in a previous operation, so that you can obtain the next page\n of results.</p>")),
ListTablesInputLimit.schema.optional[ListTablesInput]("Limit", _.limit).addHints(smithy.api.Documentation("<p>A maximum number of table names to return. If this parameter is not specified, the limit is 100.</p>")),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import smithy4s.Hints
import smithy4s.Schema
import smithy4s.ShapeId
import smithy4s.ShapeTag
import smithy4s.optics.Lens
import smithy4s.schema.Schema.struct

/** <p>Represents the output of a <code>ListTables</code> operation.</p>
Expand All @@ -27,6 +28,11 @@ object ListTablesOutput extends ShapeTag.Companion[ListTablesOutput] {
smithy.api.Documentation("<p>Represents the output of a <code>ListTables</code> operation.</p>"),
)

object Optics {
val tableNames = Lens[ListTablesOutput, Option[List[TableName]]](_.tableNames)(n => a => a.copy(tableNames = n))
val lastEvaluatedTableName = Lens[ListTablesOutput, Option[TableName]](_.lastEvaluatedTableName)(n => a => a.copy(lastEvaluatedTableName = n))
}

implicit val schema: Schema[ListTablesOutput] = struct(
TableNameList.underlyingSchema.optional[ListTablesOutput]("TableNames", _.tableNames).addHints(smithy.api.Documentation("<p>The names of the tables associated with the current account at the current endpoint. The maximum size of this array is 100.</p>\n <p>If <code>LastEvaluatedTableName</code> also appears in the output, you can use this value as the\n <code>ExclusiveStartTableName</code> parameter in a subsequent <code>ListTables</code> request and\n obtain the next page of results.</p>")),
TableName.schema.optional[ListTablesOutput]("LastEvaluatedTableName", _.lastEvaluatedTableName).addHints(smithy.api.Documentation("<p>The name of the last table in the current page of results. Use this value as the\n <code>ExclusiveStartTableName</code> in a new request to obtain the next page of results, until\n all the table names are returned.</p>\n <p>If you do not receive a <code>LastEvaluatedTableName</code> value in the response, this means that\n there are no more table names to be retrieved.</p>")),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import smithy4s.Schema
import smithy4s.ShapeId
import smithy4s.ShapeTag
import smithy4s.Timestamp
import smithy4s.optics.Lens
import smithy4s.schema.Schema.boolean
import smithy4s.schema.Schema.long
import smithy4s.schema.Schema.string
Expand All @@ -17,6 +18,22 @@ object Attributes extends ShapeTag.Companion[Attributes] {

val hints: Hints = Hints.empty

object Optics {
val user = Lens[Attributes, String](_.user)(n => a => a.copy(user = n))
val public = Lens[Attributes, Boolean](_.public)(n => a => a.copy(public = n))
val size = Lens[Attributes, Long](_.size)(n => a => a.copy(size = n))
val creationDate = Lens[Attributes, Timestamp](_.creationDate)(n => a => a.copy(creationDate = n))
val region = Lens[Attributes, String](_.region)(n => a => a.copy(region = n))
val queryable = Lens[Attributes, Option[Boolean]](_.queryable)(n => a => a.copy(queryable = n))
val queryableLastChange = Lens[Attributes, Option[Timestamp]](_.queryableLastChange)(n => a => a.copy(queryableLastChange = n))
val blockPublicAccess = Lens[Attributes, Option[Boolean]](_.blockPublicAccess)(n => a => a.copy(blockPublicAccess = n))
val permissions = Lens[Attributes, Option[List[Permission]]](_.permissions)(n => a => a.copy(permissions = n))
val tags = Lens[Attributes, Option[List[String]]](_.tags)(n => a => a.copy(tags = n))
val backedUp = Lens[Attributes, Option[Boolean]](_.backedUp)(n => a => a.copy(backedUp = n))
val metadata = Lens[Attributes, Option[List[Metadata]]](_.metadata)(n => a => a.copy(metadata = n))
val encryption = Lens[Attributes, Option[Encryption]](_.encryption)(n => a => a.copy(encryption = n))
}

implicit val schema: Schema[Attributes] = struct(
string.required[Attributes]("user", _.user).addHints(smithy.api.Required()),
boolean.required[Attributes]("public", _.public).addHints(smithy.api.Required()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import smithy4s.Hints
import smithy4s.Schema
import smithy4s.ShapeId
import smithy4s.ShapeTag
import smithy4s.optics.Lens
import smithy4s.schema.Schema.string
import smithy4s.schema.Schema.struct

Expand All @@ -13,6 +14,12 @@ object CreateObjectInput extends ShapeTag.Companion[CreateObjectInput] {

val hints: Hints = Hints.empty

object Optics {
val key = Lens[CreateObjectInput, String](_.key)(n => a => a.copy(key = n))
val bucketName = Lens[CreateObjectInput, String](_.bucketName)(n => a => a.copy(bucketName = n))
val payload = Lens[CreateObjectInput, S3Object](_.payload)(n => a => a.copy(payload = n))
}

implicit val schema: Schema[CreateObjectInput] = struct(
string.required[CreateObjectInput]("key", _.key).addHints(smithy.api.HttpLabel(), smithy.api.Required()),
string.required[CreateObjectInput]("bucketName", _.bucketName).addHints(smithy.api.HttpLabel(), smithy.api.Required()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import smithy4s.Hints
import smithy4s.Schema
import smithy4s.ShapeId
import smithy4s.ShapeTag
import smithy4s.optics.Lens
import smithy4s.schema.Schema.string
import smithy4s.schema.Schema.struct

Expand All @@ -13,6 +14,11 @@ object Creds extends ShapeTag.Companion[Creds] {

val hints: Hints = Hints.empty

object Optics {
val user = Lens[Creds, Option[String]](_.user)(n => a => a.copy(user = n))
val key = Lens[Creds, Option[String]](_.key)(n => a => a.copy(key = n))
}

implicit val schema: Schema[Creds] = struct(
string.optional[Creds]("user", _.user),
string.optional[Creds]("key", _.key),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import smithy4s.Schema
import smithy4s.ShapeId
import smithy4s.ShapeTag
import smithy4s.Timestamp
import smithy4s.optics.Lens
import smithy4s.schema.Schema.string
import smithy4s.schema.Schema.struct
import smithy4s.schema.Schema.timestamp
Expand All @@ -15,6 +16,12 @@ object Encryption extends ShapeTag.Companion[Encryption] {

val hints: Hints = Hints.empty

object Optics {
val user = Lens[Encryption, Option[String]](_.user)(n => a => a.copy(user = n))
val date = Lens[Encryption, Option[Timestamp]](_.date)(n => a => a.copy(date = n))
val metadata = Lens[Encryption, Option[EncryptionMetadata]](_.metadata)(n => a => a.copy(metadata = n))
}

implicit val schema: Schema[Encryption] = struct(
string.optional[Encryption]("user", _.user),
timestamp.optional[Encryption]("date", _.date).addHints(smithy.api.TimestampFormat.EPOCH_SECONDS.widen),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import smithy4s.Hints
import smithy4s.Schema
import smithy4s.ShapeId
import smithy4s.ShapeTag
import smithy4s.optics.Lens
import smithy4s.schema.Schema.boolean
import smithy4s.schema.Schema.string
import smithy4s.schema.Schema.struct
Expand All @@ -14,6 +15,12 @@ object EncryptionMetadata extends ShapeTag.Companion[EncryptionMetadata] {

val hints: Hints = Hints.empty

object Optics {
val system = Lens[EncryptionMetadata, Option[String]](_.system)(n => a => a.copy(system = n))
val credentials = Lens[EncryptionMetadata, Option[Creds]](_.credentials)(n => a => a.copy(credentials = n))
val partial = Lens[EncryptionMetadata, Option[Boolean]](_.partial)(n => a => a.copy(partial = n))
}

implicit val schema: Schema[EncryptionMetadata] = struct(
string.optional[EncryptionMetadata]("system", _.system),
Creds.schema.optional[EncryptionMetadata]("credentials", _.credentials),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import smithy4s.Schema
import smithy4s.ShapeId
import smithy4s.ShapeTag
import smithy4s.Timestamp
import smithy4s.optics.Lens
import smithy4s.schema.Schema.boolean
import smithy4s.schema.Schema.string
import smithy4s.schema.Schema.struct
Expand All @@ -16,6 +17,14 @@ object Metadata extends ShapeTag.Companion[Metadata] {

val hints: Hints = Hints.empty

object Optics {
val contentType = Lens[Metadata, Option[String]](_.contentType)(n => a => a.copy(contentType = n))
val lastModified = Lens[Metadata, Option[Timestamp]](_.lastModified)(n => a => a.copy(lastModified = n))
val checkSum = Lens[Metadata, Option[String]](_.checkSum)(n => a => a.copy(checkSum = n))
val pendingDeletion = Lens[Metadata, Option[Boolean]](_.pendingDeletion)(n => a => a.copy(pendingDeletion = n))
val etag = Lens[Metadata, Option[String]](_.etag)(n => a => a.copy(etag = n))
}

implicit val schema: Schema[Metadata] = struct(
string.optional[Metadata]("contentType", _.contentType),
timestamp.optional[Metadata]("lastModified", _.lastModified).addHints(smithy.api.TimestampFormat.EPOCH_SECONDS.widen),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import smithy4s.Hints
import smithy4s.Schema
import smithy4s.ShapeId
import smithy4s.ShapeTag
import smithy4s.optics.Lens
import smithy4s.schema.Schema.boolean
import smithy4s.schema.Schema.struct

Expand All @@ -13,6 +14,12 @@ object Permission extends ShapeTag.Companion[Permission] {

val hints: Hints = Hints.empty

object Optics {
val read = Lens[Permission, Option[Boolean]](_.read)(n => a => a.copy(read = n))
val write = Lens[Permission, Option[Boolean]](_.write)(n => a => a.copy(write = n))
val directory = Lens[Permission, Option[Boolean]](_.directory)(n => a => a.copy(directory = n))
}

implicit val schema: Schema[Permission] = struct(
boolean.optional[Permission]("read", _.read),
boolean.optional[Permission]("write", _.write),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import smithy4s.Hints
import smithy4s.Schema
import smithy4s.ShapeId
import smithy4s.ShapeTag
import smithy4s.optics.Lens
import smithy4s.schema.Schema.bytes
import smithy4s.schema.Schema.string
import smithy4s.schema.Schema.struct
Expand All @@ -15,6 +16,13 @@ object S3Object extends ShapeTag.Companion[S3Object] {

val hints: Hints = Hints.empty

object Optics {
val id = Lens[S3Object, String](_.id)(n => a => a.copy(id = n))
val owner = Lens[S3Object, String](_.owner)(n => a => a.copy(owner = n))
val attributes = Lens[S3Object, Attributes](_.attributes)(n => a => a.copy(attributes = n))
val data = Lens[S3Object, ByteArray](_.data)(n => a => a.copy(data = n))
}

implicit val schema: Schema[S3Object] = struct(
string.required[S3Object]("id", _.id).addHints(smithy.api.Required()),
string.required[S3Object]("owner", _.owner).addHints(smithy.api.Required()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import smithy4s.Hints
import smithy4s.Schema
import smithy4s.ShapeId
import smithy4s.ShapeTag
import smithy4s.optics.Lens
import smithy4s.schema.Schema.string
import smithy4s.schema.Schema.struct

Expand All @@ -13,6 +14,12 @@ object SendStringInput extends ShapeTag.Companion[SendStringInput] {

val hints: Hints = Hints.empty

object Optics {
val key = Lens[SendStringInput, String](_.key)(n => a => a.copy(key = n))
val bucketName = Lens[SendStringInput, String](_.bucketName)(n => a => a.copy(bucketName = n))
val body = Lens[SendStringInput, String](_.body)(n => a => a.copy(body = n))
}

implicit val schema: Schema[SendStringInput] = struct(
string.required[SendStringInput]("key", _.key).addHints(smithy.api.HttpLabel(), smithy.api.Required()),
string.required[SendStringInput]("bucketName", _.bucketName).addHints(smithy.api.HttpLabel(), smithy.api.Required()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import smithy4s.Hints
import smithy4s.Schema
import smithy4s.ShapeId
import smithy4s.ShapeTag
import smithy4s.optics.Lens
import smithy4s.schema.Schema.struct

/** @param astring
Expand All @@ -15,6 +16,10 @@ object AStructure extends ShapeTag.Companion[AStructure] {

val hints: Hints = Hints.empty

object Optics {
val astring = Lens[AStructure, AString](_.astring)(n => a => a.copy(astring = n))
}

implicit val schema: Schema[AStructure] = struct(
AString.schema.required[AStructure]("astring", _.astring).addHints(smithy.api.Default(smithy4s.Document.fromString("\"Hello World\" with \"quotes\""))),
){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import smithy4s.Schema
import smithy4s.ShapeId
import smithy4s.ShapeTag
import smithy4s.example.common.BrandList
import smithy4s.optics.Lens
import smithy4s.schema.Schema.struct

final case class AddBrandsInput(brands: Option[List[String]] = None)
Expand All @@ -13,6 +14,10 @@ object AddBrandsInput extends ShapeTag.Companion[AddBrandsInput] {

val hints: Hints = Hints.empty

object Optics {
val brands = Lens[AddBrandsInput, Option[List[String]]](_.brands)(n => a => a.copy(brands = n))
}

implicit val schema: Schema[AddBrandsInput] = struct(
BrandList.underlyingSchema.optional[AddBrandsInput]("brands", _.brands),
){
Expand Down
Loading