softwaremill / softwaremill/tapir

[Feature] deriving encodedNames from an ordered list

Open
#2,943 8 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Scala
Stars
1.5k
Forks
468
Avg merge
5h 37m
Merged PRs (30d)
34

Description

I think it would be nice if we can do something like this:

case class SomeThing(data: String, typeStr: String, uid: String, )

implicit lazy val sSomeThing: Schema[SomeThing] = Schema.derivedWithEncodedTypes3("data", "type", "id")

Similar is being done by Circe here:

import io.circe.{ Decoder, Encoder }

case class User(id: Long, firstName: String, lastName: String)

implicit val decodeUser: Decoder[User] = Decoder.forProduct3("id", "first_name", "last_name")(User.apply)
implicit val encodeUser: Encoder[User] = Encoder.forProduct3("id", "first_name", "last_name")(u => (u.id, u.firstName, u.lastName))

Alternatively, using a list, instead of having to specify the number of arguments, would be even better, like this:

val SomeThingList = List("data", "type", "id")

implicit lazy val sSomeThing: Schema[SomeThing] = Schema.derivedWithEncodedTypes3("data", "type", "id")

Perhaps something like this, which seems to work for me:

def makeSchema[T](schema: Schema[T], list: List[String]) = {
  val s = schema.schemaType match {
    case s @ SProduct(_) =>
      s.copy(fields = s.fields.mapWithIndex { case (field, i) => SProductField[T, field.FieldType](field.name.copy(encodedName = list.get(i).get), field.schema, field.get) })
    case x => x
  }
  schema.copy(schemaType = s)
}

and used like this:

makeSchema(Schema.derived[SomeThing], List("data", "id", "type")

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with Schema.derived and the existing derivedWithEncodedTypes3 entry point, then compare the proposed ordered-list approach with Circe's forProduct3 helpers. The issue is complete when the supported API and behavior for deriving encoded names from an ordered list are defined and validated.

Written by the indexing model from the issue text.

Assessment

Tech stack
scala
Domain
api
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.