Skip to content

Instantly share code, notes, and snippets.

@sdboyer
Last active October 15, 2021 04:35
Show Gist options
  • Save sdboyer/3051551c177a3b756a0b42f44483913c to your computer and use it in GitHub Desktop.
Save sdboyer/3051551c177a3b756a0b42f44483913c to your computer and use it in GitHub Desktop.
Simple example scuemata
import "github.com/grafana/scuemata"
// Make the whole file a scuemata.Family
scuemata.Family
lineages: [{ // Lineage 0
schemas: [{ // Schema 0.0
boolish: "true" | "false" | bool | string
}]
},
{ // Lineage 1
schemas: [{ // Schema 1.0
properbool: bool
}]
// Lineages beyond the first must define lenses.
// This lens defines forward translation, from lineage 0 into lineage 1.
lenses: forward: {
// scuemata.Family defines and enforces these tail-and-head mappings. Helper funcs
// take (valid) input objects and map them onto "from".
to: lineages[1].schemas[0]
from: lineages[0].schemas[0]
out: to & rel
// rel contains the actual schema mapping logic. Lots of choice here - and room for error.
rel: {
if ((from.boolish & string) != _|_) {
// Defaulting to false would be a case-specific
// choice the scuemata author makes
properbool: from.boolish == "true"
}
if ((from.boolish & bool) != _|_) {
properbool: from.boolish
}
}
// Lacunae are gaps in translation - in the logic in rel.
lacunae: [
// ONLY emit a lacuna if the translated object actually had a weird string value.
if ((from.boolish & string) != _|_) && ((from.boolish & ("true" | "false")) == _|_) {
// Definition of Lacuna still very much in flux
scuemata.#Lacuna & {
path: ["boolish"] // Path(s) to problem field(s)
values: [from.boolish] // Actual problem value
type: scuemata.InputTooOpen // A flag from some TBD taxonomy of lacuna types
}
}
]
}
// This lens defines reverse translation, from lineage 1 back into lineage 0.
lenses: reverse: {
to: lineages[0].schemas[0]
from: lineages[1].schemas[0]
out: to & rel
rel: {
// Preserving exact original input is firmly a non-goal.
boolish: from.properbool
}
lacunae: []
}
}]
{
"input": {
"btrue": {
"boolish": true
},
"weird": {
"boolish": "weird"
},
"strue": {
"boolish": "true"
},
"sfalse": {
"boolish": "false"
}
},
"output": {
"btrue": {
"inputval": true,
"forwardval": true,
"reverseval": true
},
"weird": {
"inputval": "weird",
"forwardval": false,
"reverseval": false,
"forwardlacunae": [
{
"path": [
"boolish"
],
"values": [
"weird"
]
}
]
},
"strue": {
"inputval": "true",
"forwardval": true,
"reverseval": true
},
"sfalse": {
"inputval": "false",
"forwardval": false,
"reverseval": false
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment