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

Feature: Add iron's RefinedTypeOps support #3245

Merged
merged 3 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import io.github.iltotore.iron.Constraint
import io.github.iltotore.iron.:|
import io.github.iltotore.iron.refineEither
import io.github.iltotore.iron.refineOption
import io.github.iltotore.iron.RefinedTypeOps
import io.github.iltotore.iron.RefinedTypeOpsImpl
import io.github.iltotore.iron.constraint.any.*
import io.github.iltotore.iron.constraint.string.*
import io.github.iltotore.iron.constraint.collection.*
Expand Down Expand Up @@ -32,7 +34,7 @@ trait TapirCodecIron extends DescriptionWitness with LowPriorityValidatorForPred
): Schema[Value :| Predicate] =
vSchema.validate(validatorTranslation.validator).map[Value :| Predicate](v => v.refineOption[Predicate])(identity)

inline given [Representation, Value, Predicate, CF <: CodecFormat](using
inline given ironTypeCodec[Representation, Value, Predicate, CF <: CodecFormat](using
inline tm: Codec[Representation, Value, CF],
inline constraint: Constraint[Value, Predicate],
inline validatorTranslation: ValidatorForPredicate[Value, Predicate]
Expand All @@ -47,6 +49,12 @@ trait TapirCodecIron extends DescriptionWitness with LowPriorityValidatorForPred
}
}(identity)

given refinedTypeSchema[T](using m: RefinedTypeOps.Mirror[T], ev: Schema[m.IronType]): Schema[T] =
ev.asInstanceOf[Schema[T]]

given refinedTypeCodec[R, T, CF <: CodecFormat] (using m: RefinedTypeOps.Mirror[T], ev: Codec[R, m.IronType, CF]): Codec[R, T, CF] =
ev.asInstanceOf[Codec[R, T, CF]]

inline given (using
inline vSchema: Schema[String],
inline constraint: Constraint[String, ValidUUID],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ import io.github.iltotore.iron.constraint.all.*
import sttp.tapir.Validator
import sttp.tapir.ValidationError

// Opaque aliases can't be included into class bodies
type RefinedIntConstraint = Interval.ClosedOpen[0, 10]
opaque type RefinedInt <: Int = Int :| RefinedIntConstraint
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't the opaque type alias be defined in a separate file, so that it's alias is not visible inside this file (and hence inside the test)? Otherwise we might be testing behaviour similar to a normal type alias?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, I moved the opaque definition to its own file.

object RefinedInt extends RefinedTypeOpsImpl[Int, RefinedIntConstraint, RefinedInt]

class TapirCodecIronTestScala3 extends AnyFlatSpec with Matchers {

val schema: Schema[Double :| Positive] = summon[Schema[Double :| Positive]]
Expand Down Expand Up @@ -215,4 +220,8 @@ class TapirCodecIronTestScala3 extends AnyFlatSpec with Matchers {
}
}

"Instances for opaque refined type" should "correctly derived" in:
summon[Schema[RefinedInt]]
summon[Codec[String, RefinedInt, TextPlain]]

}
Loading