Skip to content

Instantly share code, notes, and snippets.

@ShahOdin
Last active September 27, 2021 00:32
Show Gist options
  • Save ShahOdin/428c64b91984c2d55160a98e53fa40a5 to your computer and use it in GitHub Desktop.
Save ShahOdin/428c64b91984c2d55160a98e53fa40a5 to your computer and use it in GitHub Desktop.
sealed trait NaturalNumber
sealed trait _0 extends NaturalNumber
sealed trait _1 extends _0
sealed trait _2 extends _1
sealed trait _3 extends _2
sealed trait _4 extends _3
sealed trait _5 extends _4
sealed trait _6 extends _5
sealed trait _7 extends _6
sealed trait _8 extends _7
sealed trait _9 extends _8
sealed trait _10 extends _9
//////////////////////////////////////////////////
type IsEqual[N <: NaturalNumber, M <: NaturalNumber] = N =:= M
type IsLessOrEqual[N <: NaturalNumber, M <: NaturalNumber] = N <:< M
//////////////////////////////////////////////////
sealed trait VersionNumber{
type Nat <:NaturalNumber
}
object V0 extends VersionNumber{ override type Nat = _0 }
object V1 extends VersionNumber{ override type Nat = _1 }
object V2 extends VersionNumber{ override type Nat = _2 }
object V3 extends VersionNumber{ override type Nat = _3 }
object V4 extends VersionNumber{ override type Nat = _4 }
object V5 extends VersionNumber{ override type Nat = _5 }
object V6 extends VersionNumber{ override type Nat = _6 }
object V7 extends VersionNumber{ override type Nat = _7 }
object V8 extends VersionNumber{ override type Nat = _8 }
object V9 extends VersionNumber{ override type Nat = _9 }
object V10 extends VersionNumber{ override type Nat = _10 }
//////////////////////////////////////////////////
trait ResourceManifest {
def getResource: Int
type Major <: NaturalNumber
type Minor <: NaturalNumber
}
object Resource42 extends ResourceManifest {
def getResource: Int = 42
type Major = _2
type Minor = _3
}
//it takes VersionNumbers as parameters and implicitly checks if the version Numbers "match" that of the manifest. Fails to compile if they don't@implicitNotFound(msg = "The required version is not compatible with the specified version information inside the resource manifest")
def getResource(manifest: ResourceManifest)(maj: VersionNumber, min: VersionNumber)
(implicit
maj_check: manifest.Major IsEqual maj.Nat,
min_check: manifest.Minor IsLessOrEqual min.Nat
) = manifest.getResource
//getResource(Resource42)(V1,V0)// want version 1.0 => fails to compile due to incompatible Major version
getResource(Resource42)(V2, V2) // want version 2.2 => compiles
getResource(Resource42)(V2, V3) // want version 2.3 => compiles
//getResource(Resource42)(V2,V4) // want version 2.4 => fails to compile as that version is not available yet.
//getResource(Resource42)(V3,V0)// want version 3.0 => fails to compile due to incompatible Major version
@ShahOdin
Copy link
Author

ShahOdin commented Jun 22, 2018

This is a mechanism for versioning mono-repo services. instead of relying on a versioned client or embeding the version information in a header and then realising the server has rejected/deprecated your request.
we can instead make services provide a series of "manifest" objects which contain the type and version information and the client code can fail compilation if the version information is not right.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment