Restructured CUE resource definitions (struct, attributes)
This commit is contained in:
@@ -14,7 +14,7 @@ var (
|
||||
// {{ .ident }}Table represents {{ .identPlural }} store table
|
||||
//
|
||||
// This value is auto-generated
|
||||
{{ .ident }}Table = goqu.T({{ printf "%q" .settings.rdbms.table }})
|
||||
{{ .ident }}Table = goqu.T({{ printf "%q" .modelIdent }})
|
||||
|
||||
// {{ .ident }}SelectQuery assembles select query for fetching {{ .identPlural }}
|
||||
//
|
||||
|
||||
@@ -5,8 +5,10 @@ import (
|
||||
)
|
||||
|
||||
#Model: {
|
||||
// Each field can be
|
||||
[name=_]: {"name": name} & #ModelAttribute
|
||||
ident: string
|
||||
attributes: {
|
||||
[name=_]: {"name": name} & #ModelAttribute
|
||||
}
|
||||
}
|
||||
|
||||
// logic in struct fields is a bit different
|
||||
@@ -38,7 +40,7 @@ import (
|
||||
#ModelAttributeJsonTag
|
||||
}
|
||||
|
||||
IdField: #ModelAttribute & {
|
||||
IdField: {
|
||||
// Expecting ID field to always have name ID
|
||||
name: "id"
|
||||
expIdent: "ID"
|
||||
@@ -49,7 +51,7 @@ IdField: #ModelAttribute & {
|
||||
goType: "uint64"
|
||||
}
|
||||
|
||||
HandleField: #ModelAttribute & {
|
||||
HandleField: {
|
||||
// Expecting ID field to always have name handle
|
||||
name: "handle"
|
||||
unique: true
|
||||
@@ -58,12 +60,12 @@ HandleField: #ModelAttribute & {
|
||||
goType: "string"
|
||||
}
|
||||
|
||||
SortableTimestampField: #ModelAttribute & {
|
||||
SortableTimestampField: {
|
||||
sortable: true
|
||||
goType: "time.Time"
|
||||
}
|
||||
|
||||
SortableTimestampNilField: #ModelAttribute & {
|
||||
SortableTimestampNilField: {
|
||||
sortable: true
|
||||
goType: "*time.Time"
|
||||
}
|
||||
|
||||
+10
-12
@@ -18,11 +18,18 @@ import (
|
||||
// Fully qualified resource name
|
||||
fqrn: string | *(platform + "::" + component + ":" + handle)
|
||||
|
||||
model: #Model
|
||||
model: #Model & {
|
||||
// use resource handle (plural) as model ident as default
|
||||
// model ident represents a db table or a container name
|
||||
ident: string | *"\(strings.Replace(handle, "-", "_", -1))s"
|
||||
}
|
||||
|
||||
filter: {
|
||||
"expIdent": #expIdent | *"\(expIdent)Filter"
|
||||
|
||||
model: #Model
|
||||
struct: {
|
||||
[name=_]: {"name": name} & #ModelAttribute
|
||||
}
|
||||
|
||||
// generate filtering by-nil-state for the specified fields
|
||||
"byNilState": [...string]
|
||||
@@ -86,7 +93,7 @@ import (
|
||||
|
||||
api?: {
|
||||
lookups: [...{
|
||||
_expFields: [ for f in fields {strings.ToTitle(model[f].expIdent)}]
|
||||
_expFields: [ for f in fields {strings.ToTitle(model.attributes[f].expIdent)}]
|
||||
|
||||
"expIdent": "Lookup\(store.expIdent)By" + strings.Join(_expFields, "")
|
||||
description: string | *""
|
||||
@@ -108,15 +115,6 @@ import (
|
||||
return: [...string]
|
||||
}]
|
||||
}
|
||||
|
||||
settings: {
|
||||
defaultOrder: [...{ field: string, descending: bool | *false }]
|
||||
|
||||
rdbms: {
|
||||
// use resource handle (plural) as RDBMS table name as default
|
||||
table: string | *"\(strings.Replace(handle, "-", "_", -1))s"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+14
-14
@@ -16,11 +16,13 @@ _StoreResource: {
|
||||
identPlural: res.store.identPlural
|
||||
expIdent: res.store.expIdent
|
||||
expIdentPlural: res.store.expIdentPlural
|
||||
modelIdent: res.model.ident
|
||||
goType: "\(typePkg).\(res.expIdent)"
|
||||
goSetType: "\(typePkg).\(res.expIdent)Set"
|
||||
goFilterType: "\(typePkg).\(res.filter.expIdent)"
|
||||
|
||||
struct: [ for f in res.model if f.store {
|
||||
|
||||
struct: [ for f in res.model.attributes if f.store {
|
||||
"ident": f.ident
|
||||
"expIdent": f.expIdent
|
||||
"storeIdent": f.storeIdent
|
||||
@@ -32,17 +34,17 @@ _StoreResource: {
|
||||
|
||||
filter: {
|
||||
// query fields as defined in struct
|
||||
"query": [ for name in res.filter.query {res.model[name]}],
|
||||
"query": [ for name in res.filter.query {res.model.attributes[name]}],
|
||||
|
||||
// filter by nil state as defined in filter
|
||||
"byNilState": [ for name in res.filter.byNilState {res.filter.model[name]}]
|
||||
"byNilState": [ for name in res.filter.byNilState {res.filter.struct[name]}]
|
||||
|
||||
// filter by false as defined in filter
|
||||
"byFalseState": [ for name in res.filter.byFalseState {res.filter.model[name]}]
|
||||
"byFalseState": [ for name in res.filter.byFalseState {res.filter.struct[name]}]
|
||||
|
||||
// filter by value as defined in filter
|
||||
// @todo this should be pulled from the struct
|
||||
"byValue": [ for name in res.filter.byValue {res.filter.model[name]}]
|
||||
"byValue": [ for name in res.filter.byValue {res.filter.struct[name]}]
|
||||
"byLabel": res.features.labels
|
||||
"byFlag": res.features.flags
|
||||
}
|
||||
@@ -67,7 +69,7 @@ _StoreResource: {
|
||||
}
|
||||
|
||||
deleteByPK: {
|
||||
primaryKeys: [ for f in res.model if f.primaryKey {f} ]
|
||||
primaryKeys: [ for f in res.model.attributes if f.primaryKey {f} ]
|
||||
_pkExpNames: strings.Join([ for f in primaryKeys { f.expIdent } ], "")
|
||||
"expFnIdent": "Delete\(res.store.expIdent)By\(_pkExpNames)"
|
||||
}
|
||||
@@ -85,7 +87,7 @@ _StoreResource: {
|
||||
// Copy all relevant fields from the struct
|
||||
"args": [
|
||||
for name in l.fields {
|
||||
let f = res.model[name]
|
||||
let f = res.model.attributes[name]
|
||||
|
||||
"ident": f.ident
|
||||
"storeIdent": f.storeIdent
|
||||
@@ -126,7 +128,7 @@ _StoreResource: {
|
||||
"fnIdent": "sortable\(expIdent)Fields"
|
||||
|
||||
fields: {
|
||||
for f in res.model if f.sortable || f.unique || f.primaryKey {
|
||||
for f in res.model.attributes if f.sortable || f.unique || f.primaryKey {
|
||||
{
|
||||
"\(strings.ToLower(f.name))": f.name
|
||||
"\(strings.ToLower(f.ident))": f.name
|
||||
@@ -140,8 +142,8 @@ _StoreResource: {
|
||||
|
||||
"fnIdent": "collect\(expIdent)CursorValues"
|
||||
|
||||
fields: [ for f in res.model if f.sortable || f.unique || f.primaryKey {f} ]
|
||||
primaryKeys: [ for f in res.model if f.primaryKey {f} ]
|
||||
fields: [ for f in res.model.attributes if f.sortable || f.unique || f.primaryKey {f} ]
|
||||
primaryKeys: [ for f in res.model.attributes if f.primaryKey {f} ]
|
||||
}
|
||||
|
||||
checkConstraints: {
|
||||
@@ -152,9 +154,9 @@ _StoreResource: {
|
||||
checks: [
|
||||
for lookup in res.store.api.lookups if lookup.constraintCheck {
|
||||
lookupFnIdent: lookup.expIdent
|
||||
fields: [ for name in lookup.fields {res.model[name]}]
|
||||
fields: [ for name in lookup.fields {res.model.attributes[name]}]
|
||||
nullConstraint: [
|
||||
for f in res.model if list.Contains(lookup.nullConstraint, f.name) {
|
||||
for f in res.model.attributes if list.Contains(lookup.nullConstraint, f.name) {
|
||||
"expIdent": f.expIdent
|
||||
},
|
||||
]
|
||||
@@ -163,8 +165,6 @@ _StoreResource: {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
settings: res.store.settings
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user