Skip to content

A type driven approach to string interpolation, aiming at consistent, secure, and only-human-readable logs and console outputs !

License

Notifications You must be signed in to change notification settings

afsalthaj/safe-string-interpolation

Repository files navigation

Typesafe Interpolation

Build Status Gitter Maven Central

An insanely simple type driven approach to string interpolation, aiming at consistent, secure, and only-human-readable logs and console outputs, and for safe string operations !

@ import $ivy.`io.github.afsalthaj::safe-string-interpolation:2.1.3`
import $ivy.$

@ import com.thaj.safe.string.interpolator._
import com.thaj.safe.string.interpolator._

@ case class Port(int: Int) extends AnyVal
defined class Port

@ val port = Port(1)
port: Port = Port(1)

@ ss"Db details: Port is ${port}"
cmd7.sc:1: could not find implicit value for parameter ev: com.thaj.safe.string.interpolator.Safe[ammonite.$sess.cmd5.Port]
val res7 = ss"Db details: Port is ${port}"
           ^
Compilation Failed

@ ss"Db details: Port is ${port.int}"
res7: SafeString = SafeString("Db details: Port is 1")

// Another example
@ case class Prefix(value: String) 
defined class Prefix

@ case class BucketName(value: String) 
defined class BucketName

@ val prefix = Prefix("foo") 
prefix: Prefix = Prefix("foo")

@ val bucketName = BucketName("6666-bucket") 
bucketName: BucketName = BucketName("6666-bucket")

@ val path = s"${bucketName}/${prefix}" 
path: String = "BucketName(6666-bucket)/Prefix(foo)"

@ val path = ss"${bucketName}/${prefix}" 
cmd7.sc:1: could not find implicit value for parameter ev: com.thaj.safe.string.interpolator.Safe[ammonite.$sess.cmd2.BucketName]
val path = ss"${bucketName}/${prefix}"
           ^
Compilation Failed


// Autoderivation module
@ import instances._
import instances._

@ ss"Db details: Port is ${port}"
res9: SafeString = SafeString("Db details: Port is { int : 1 }")

Checkout the project website for more features and possibilities.