Skip to content

Commit

Permalink
all: cue fix
Browse files Browse the repository at this point in the history
  • Loading branch information
sam boyer committed Nov 11, 2022
1 parent c2cee21 commit 076bfe1
Show file tree
Hide file tree
Showing 6 changed files with 293 additions and 294 deletions.
147 changes: 73 additions & 74 deletions crd/crd.cue
Original file line number Diff line number Diff line change
@@ -1,98 +1,97 @@
package crd

import (
"list"
"strings"
"github.com/grafana/thema"
"list"
"strings"
"github.com/grafana/thema"
)

// CRD transforms a lineage into a Kubernetes custom resource definition, or a series thereof.
#CRD: {
_sv: [<len(lin.seqs), <len(lin.seqs[_sv[0]].schemas)]
served: [..._sv]
storage: _sv
lin: thema.#Lineage
_sv: [<len(lin.seqs), <len(lin.seqs[_sv[0]].schemas)]
served: [..._sv]
storage: _sv
lin: thema.#Lineage

// Additional metadata necessary to convert a thema lineage into a
// Kubernetes Custom Resource Definition (CRD).
crdspec = spec: {
// Additional metadata necessary to convert a thema lineage into a
// Kubernetes Custom Resource Definition (CRD).
crdspec = spec: {
// scope indicates whether the defined custom resource is cluster-
// or namespace-scoped.
scope: "Namespaced" | "Cluster"
scope: "Namespaced" | "Cluster"

// group is the API group of the defined custom resource. The
// custom resources are served under `/apis/<group>/...`.
// Ex.: stable.example.com
group: string
// Ex.: stable.example.com
group: string

names: {
// categories is a list of grouped resources this custom resource
// belongs to (e.g. 'all'). This is published in API discovery
// documents, and used by clients to support invocations like
// `kubectl get all`.
categories?: [...string]
names: {
// categories is a list of grouped resources this custom resource
// belongs to (e.g. 'all'). This is published in API discovery
// documents, and used by clients to support invocations like
// `kubectl get all`.
categories?: [...string]

// kind is the serialized kind of the resource. It is normally
// CamelCase and singular. Custom resource instances will use
// this value as the `kind` attribute in API calls.
// TODO default this to thema name
kind: string | *lin.name
// kind is the serialized kind of the resource. It is normally
// CamelCase and singular. Custom resource instances will use
// this value as the `kind` attribute in API calls.
// TODO default this to thema name
kind: string | *lin.name

// listKind is the serialized kind of the list for this resource.
listKind: string | *"\(kind)List"
// listKind is the serialized kind of the list for this resource.
listKind: string | *"\(kind)List"

// plural is the plural name of the resource to serve. The custom
// resources are served under
// `/apis/<group>/<version>/.../<plural>`.
plural: string | =~ #"[a-z]"# | *"\(lin.name)s"
// plural is the plural name of the resource to serve. The custom
// resources are served under
// `/apis/<group>/<version>/.../<plural>`.
plural: string | =~#"[a-z]"# | *"\(lin.name)s"

// shortNames allow shorter string to match your resource on the CLI
shortNames?: [...string]
// shortNames allow shorter string to match your resource on the CLI
shortNames?: [...string]

// singular is the singular name of the resource. It must be all
// lowercase.
singular: string | *strings.ToLower(kind)
// TODO(must) https://github.com/cue-lang/cue/issues/943
// singular: must(singular == strings.ToLower(singular), "singular form must be all lower case")
}
// Deprecated upstream, so omitted
// preserveUnknownFields: bool | *false
// singular is the singular name of the resource. It must be all
// lowercase.
singular: string | *strings.ToLower(kind)
// TODO(must) https://github.com/cue-lang/cue/issues/943
// singular: must(singular == strings.ToLower(singular), "singular form must be all lower case")
}
// Deprecated upstream, so omitted
// preserveUnknownFields: bool | *false

// conversion defines conversion settings for the CRD.
conversion?: {
// TODO for now, only allow this and not webhook, because what we
// really want to do is swap thema translation logic in for
// Scheme
strategy: "None"
}
}
conversion?: {
// TODO for now, only allow this and not webhook, because what we
// really want to do is swap thema translation logic in for
// Scheme
strategy: "None"
}
}

// The lineage, transformed into a valid CRD.
crd: {
apiVersion: "apiextensions.k8s.io/v1"
kind: "CustomResourceDefinition"
metadata: {
name: "\(crdspec.names.plural).\(crdspec.group)"
}
spec: crdspec
spec: versions: [
for seqv, seq in lin.seqs {
for schv, sch in seq.schemas {
served: list.Contains(served, [seqv, schv])
storage: [seqv, schv] == storage
name: "v\(seqv).\(schv)" // Not sure if the dot is allowed
schema: {
openAPIV3Schema: {...} // This is what needs to be filled in by the encoder
cueSchema: sch
}
}
}
]
}
// The lineage, transformed into a valid CRD.
crd: {
apiVersion: "apiextensions.k8s.io/v1"
kind: "CustomResourceDefinition"
metadata: {
name: "\(crdspec.names.plural).\(crdspec.group)"
}
spec: crdspec
spec: versions: [
for seqv, seq in lin.seqs {
for schv, sch in seq.schemas {
served: list.Contains(served, [seqv, schv])
storage: [seqv, schv] == storage
name: "v\(seqv).\(schv)" // Not sure if the dot is allowed
schema: {
openAPIV3Schema: {...} // This is what needs to be filled in by the encoder
cueSchema: sch
}
}
},
]
}

// The lineage, reformed in the shape of a custom resource validator.
crv: {
// TODO
}
// The lineage, reformed in the shape of a custom resource validator.
crv: {
// TODO
}
}

86 changes: 43 additions & 43 deletions docs/ship.cue
Original file line number Diff line number Diff line change
Expand Up @@ -5,48 +5,48 @@ import "github.com/grafana/thema"
lin: thema.#Lineage
lin: name: "Ship"
lin: seqs: [
{
schemas: [
{ // 0.0
firstfield: string
},
]
},
{
schemas: [
{ // 1.0
firstfield: string
secondfield: int
}
]
{
schemas: [
{// 0.0
firstfield: string
},
]
},
{
schemas: [
{// 1.0
firstfield: string
secondfield: int
},
]

lens: forward: {
from: seqs[0].schemas[0]
to: seqs[1].schemas[0]
rel: {
firstfield: from.firstfield
secondfield: -1
}
lacunas: [
thema.#Lacuna & {
targetFields: [{
path: "secondfield"
value: to.secondfield
}]
message: "-1 used as a placeholder value - replace with a real value before persisting!"
type: thema.#LacunaTypes.Placeholder
}
]
translated: to & rel
}
lens: reverse: {
from: seqs[1].schemas[0]
to: seqs[0].schemas[0]
rel: {
// Map the first field back
firstfield: from.firstfield
}
translated: to & rel
}
}
lens: forward: {
from: seqs[0].schemas[0]
to: seqs[1].schemas[0]
rel: {
firstfield: from.firstfield
secondfield: -1
}
lacunas: [
thema.#Lacuna & {
targetFields: [{
path: "secondfield"
value: to.secondfield
}]
message: "-1 used as a placeholder value - replace with a real value before persisting!"
type: thema.#LacunaTypes.Placeholder
},
]
translated: to & rel
}
lens: reverse: {
from: seqs[1].schemas[0]
to: seqs[0].schemas[0]
rel: {
// Map the first field back
firstfield: from.firstfield
}
translated: to & rel
}
},
]
30 changes: 15 additions & 15 deletions encoding/jsonschema/single.cue
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@ package exemplars
import "github.com/grafana/thema"

single: {
description: "Lineage containing one sequence with a single, trivial schema."
l: thema.#Lineage & {
seqs: [
{
schemas: [
{
astring: string
anint: int
abool: bool
anopt?: [number, number]
}
]
}
]
}
description: "Lineage containing one sequence with a single, trivial schema."
l: thema.#Lineage & {
seqs: [
{
schemas: [
{
astring: string
anint: int
abool: bool
anopt?: [number, number]
},
]
},
]
}
}

oi: #Single: single.l.seqs[0].schemas[0]
Loading

0 comments on commit 076bfe1

Please sign in to comment.