Skip to content

Instantly share code, notes, and snippets.

@sdboyer
Last active September 6, 2022 13:12
Show Gist options
  • Save sdboyer/a98a52c6d19919055f340bb440ab47a8 to your computer and use it in GitHub Desktop.
Save sdboyer/a98a52c6d19919055f340bb440ab47a8 to your computer and use it in GitHub Desktop.
pseudoframework for variant object refs in schemas. NOTE, untested pseudocode
// Schema of our B type
#B: {
type: string
aFieldInB: string
}
// Schema of our A type
#A: {
// someB should ultimately be resolvable to a single instance of B,
// but instances of A are allowed to accept a B literal, constraint, or id
someB: #RefOne{
typ: #B
id: string
constraint: { type?: string, aFieldInB?: string }
lit: true
}.out
}
// A proper framework would produce this, via some #Envelope type. The author of #A does not and
// should not produce the enveloped version of A.
#EnvelopedA: {
body: #A
meta: resolvedRefs: {
// same field name as in #A, but only accepts an actual #B literal, or a special
// error type indicating the reference could not be resolved. Again, a proper framework
// would derive this - i'm handwriting the results of such derivation.
someB: #B | #Missing
}
}
// some useful error fields, whatever, not trying too hard here
#Missing: {
message: string
}
// Pseudo impl of a ref framework that allows a schema to express that a particular field
// should logically contain one instance of some typed object, but the author may express the sub-object
// in one of three ways:
// - a reference, by string id (only one match should be possible)
// - a reference, by constraint on some field in the type (many may match, though still only accept one)
// - an object literal
#RefOne: {
// type/schema of the object to be referenced. At this framework level, can be any struct
typ: {...}
// If provided, accept ids and enforce that the shape of all ids used by this type must be
// some kind of string. (Caller could further constrain ids with e.g. a regex)
id?: string
// If provided, the set of fields that are allowed to be constrained on.
// Ideally, framework would verify that these are present in typ. Or, better, do a TS-style Partial<typ>
// There's a _lot_ we could do here - it's essentially a mini query language - but should start by keeping it simple.
constraint?: {...}
// Whether to accept a literal
lit: bool | *false
// Validation - a #RefOne must allow at least one of by id, by constraint, or literal.
// If none are, this unifies to bottom and the whole #RefOne instance is invalid
_must: true & ((lit) || (constraint != _|_) || (id != _|_))
// The actual disjunction which ends up in the schema. or() explodes a list into a disjunction
out: or([
// accept a literal if lit field is true
if lit { typ },
// allow schema instances to specify an id for references iff input field is set
if (id != _|_) { id },
// allow schema instances to specify constraints for reference iff input field is set
// Wrap within a field named "match" to disambiguate from the literal case
if (constraint != _|_) { match: constraint }
])
}
{
"someB": {
"type": "whatever",
"aFieldInB": "field value from object in b-foo-input.json"
}
}
{
"body": {
"someB": {
"type": "whatever",
"aFieldInB": "field value in embedded literal B"
}
},
"meta": {
"resolvedRefs": {
"someB": {
"type": "whatever",
"aFieldInB": "field value in embedded literal B"
}
}
}
}
{
"someB": {
"match": {
"type": "prometheus"
}
}
}
{
"body": {
"someB": {
"match": {
"type": "prometheus"
}
},
"meta": {
"resolvedRefs": {
"someB": {
"type": "whatever",
"aFieldInB": "field value from object in b-foo-input.json"
}
}
}
}
{
"someB": "foo"
}
{
"body": {
"someB": "foo",
},
"meta": {
"resolvedRefs": {
"someB": {
"type": "whatever",
"aFieldInB": "field value from object in b-foo-input.json"
}
}
}
}
{
"type": "prometheus",
"aFieldInB": "field value from object in b-foo-input.json"
}
{
"meta": {},
"body": {
"type": "prometheus",
"aFieldInB": "field value from object in b-foo-input.json"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment