Skip to content

Commit

Permalink
more xref testing
Browse files Browse the repository at this point in the history
Signed-off-by: Doug Davis <[email protected]>
  • Loading branch information
duglin committed Jan 16, 2025
1 parent db2f5ad commit 2c69dca
Show file tree
Hide file tree
Showing 11 changed files with 204 additions and 42 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,5 @@ TODOs:
- make sure that setting capabilities.BOOL-attrs to 'true' fails if we don't
support it. Like pagination or enforcecompatibility
- add support for shortself
- add tests for valid attribute names, at all levels, under ANY and #abc
- add tests for valid map key names, at all levels, under ANY
9 changes: 8 additions & 1 deletion cmds/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ var GitCommit string
var Port = 8080
var DBName = "registry"
var Verbose = 2
var RegistryName = "CloudEvents"

var doDelete *bool
var doRecreate *bool
var doVerify *bool
var noLoad *bool
var firstTimeDB = true

func InitDB() {
Expand Down Expand Up @@ -46,7 +48,7 @@ func InitDB() {
return
}

reg, err := registry.FindRegistry(nil, "CloudEvents")
reg, err := registry.FindRegistry(nil, RegistryName)
if err != nil {
fmt.Fprint(os.Stderr, err)
return
Expand Down Expand Up @@ -93,10 +95,15 @@ func main() {
doDelete = flag.Bool("delete", false, "Delete DB and exit")
doRecreate = flag.Bool("recreate", false, "Recreate DB, then run")
doVerify = flag.Bool("verify", false, "Exit after loading - for testing")
noLoad = flag.Bool("noload", false, "Don't load any models")
flag.IntVar(&Verbose, "v", Verbose, "Verbose level")
flag.IntVar(&Port, "p", Port, "Listen port")
flag.Parse()

if flag.NArg() > 0 {
RegistryName = flag.Arg(0)
}

log.SetVerbose(Verbose)

registry.PanicIf(GitCommit == "", "GitCommit isn't set")
Expand Down
70 changes: 52 additions & 18 deletions registry/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,10 @@ func (e *Entity) GetPP(pp *PropPath) any {
}

if pp.Len() == 1 && pp.Top() == "#resource" {
contentID := e.Get("#contentid")
results, err := Query(e.tx, `
SELECT Content
FROM ResourceContents
WHERE VersionSID=? OR
VersionSID=(SELECT eSID FROM FullTree WHERE ParentSID=? AND
PropName=? and PropValue='true')
`, e.DbSID, e.DbSID, NewPPP("isdefault").DB())
SELECT Content FROM ResourceContents WHERE VersionSID=? `,
contentID)
defer results.Close()

if err != nil {
Expand Down Expand Up @@ -451,6 +448,18 @@ func (e *Entity) eSetSave(path string, val any) error {
err = e.SetPP(pp, val)
}

// TODO - find a better place for this.
// This will set the #contentID on a Version if #resource is
// present. We need to do this otherwise the entity in the cache
// will be not have it. We can't set this in Save() when we set
// #contentid in the DB because we're not allowed to change the
// contents of the Entity while saving it.
// Should not need this once we stop calling Set("#resource".
// We should be calling Set("SINGULARid") instead
if e.Type == ENTITY_VERSION && e.Object["#resource"] != nil {
e.Object["#contentid"] = e.DbSID
}

return err
}

Expand Down Expand Up @@ -604,13 +613,23 @@ func (e *Entity) SetDBProperty(pp *PropPath, val any) error {
// Need to explicitly set #resource to nil to delete it.
if pp.Len() == 1 && pp.Top() == "#resource" {
if IsNil(val) {
// Remove the content
err = Do(e.tx, `DELETE FROM ResourceContents WHERE VersionSID=?`,
e.DbSID)
return err
if err != nil {
return err
}

// Let 'nil' value fall thru so it'll delete #resource
} else {
// Update the content

if val == "" {
// For "" delete the content from ResourceContents but
// keep #resource around. Just saving space
return nil
}

// The actual contents
err = DoOneTwo(e.tx, `
REPLACE INTO ResourceContents(VersionSID, Content)
Expand All @@ -619,11 +638,8 @@ func (e *Entity) SetDBProperty(pp *PropPath, val any) error {
return err
}

// DUG TODO for fixing xref #resource issue
// Store versionSID as #resource's value, we'll use that
// as the look-up into the ResourceContents table. We can't
// count on the VersionSID because when we're in an xref'd
// entity the VersionSID won't be correct
PanicIf(IsNil(e.NewObject["#contentid"]), "Missing cid")

val = ""
// Fall thru to normal processing so we save a placeholder
// attribute in the resource
Expand Down Expand Up @@ -1375,7 +1391,15 @@ var OrderedSpecProps = []*Attribute{
{
Name: "$resource",
internals: AttrInternals{
types: StrTypes(ENTITY_RESOURCE),
types: StrTypes(ENTITY_RESOURCE, ENTITY_VERSION),
updateFn: func(e *Entity) error {
if e.Type == ENTITY_VERSION {
if !IsNil(e.NewObject["#resource"]) {
e.NewObject["#contentid"] = e.DbSID
}
}
return nil
},
},
},
{
Expand Down Expand Up @@ -1748,6 +1772,7 @@ func (e *Entity) Save() error {

// We exclude #resource because we need to leave it around until
// a user action causes us to explictly delete it
// TODO we may need to exclude #contentid at some point too
err := Do(e.tx, "DELETE FROM Props WHERE EntitySID=? "+
"AND PropName!='"+NewPPP("#resource").DB()+"'", e.DbSID)
if err != nil {
Expand Down Expand Up @@ -2146,7 +2171,8 @@ func (e *Entity) ValidateObject(val any, origAttrs Attributes, path *PropPath) e

// For each attribute (key) in newObj, check its type
for _, key = range keys {
if key[0] == '#' {
if key[0] == '#' && path.Len() == 0 {
// Skip system attributes, but only at top level
continue
}

Expand All @@ -2161,11 +2187,11 @@ func (e *Entity) ValidateObject(val any, origAttrs Attributes, path *PropPath) e
}
}

val, ok := newObj[key]
val, keyPresent := newObj[key]

// A Default value is defined but there's no value, so set it
// and then let normal processing continue
if !IsNil(attr.Default) && (!ok || IsNil(val)) {
if !IsNil(attr.Default) && (!keyPresent || IsNil(val)) {
newObj[key] = attr.Default
}

Expand Down Expand Up @@ -2222,13 +2248,13 @@ func (e *Entity) ValidateObject(val any, origAttrs Attributes, path *PropPath) e
}

// Required but not present - note that nil means will be deleted
if attr.ClientRequired && (!ok || IsNil(val)) {
if attr.ClientRequired && (!keyPresent || IsNil(val)) {
return fmt.Errorf("Required property %q is missing",
path.P(key).UI())
}

// Not ClientRequired && not there (or being deleted)
if !attr.ClientRequired && (!ok || IsNil(val)) {
if !attr.ClientRequired && (!keyPresent || IsNil(val)) {
delete(objKeys, key)
continue
}
Expand All @@ -2240,6 +2266,14 @@ func (e *Entity) ValidateObject(val any, origAttrs Attributes, path *PropPath) e
}
}

// And finally check to make sure it's a valid attribute name,
// but only if it's actually present in the object.
if keyPresent {
if err := IsValidAttributeName(key); err != nil {
return err
}
}

// Everything is good, so remove it
delete(objKeys, key)
}
Expand Down
20 changes: 19 additions & 1 deletion registry/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,18 @@ func (g *Group) UpsertResourceWithObject(rType string, id string, vID string, ob
}
}

// Process the "meta" sub-object if there - but NOT versioninfo yet
if !IsNil(metaObj) {
_, _, err := r.UpsertMetaWithObject(metaObj, addType, false, false)
if err != nil {
if isNew {
// Needed if doing local func calls to create the Resource
// and we don't commit/rollback the tx upon failure
r.Delete()
}
return nil, false, err
}
}
// Now we have a Resource.
// Order of processing:
// - "versions" collection if there
Expand All @@ -274,6 +286,11 @@ func (g *Group) UpsertResourceWithObject(rType string, id string, vID string, ob
// - Resource level properties applied to default version IFF default
// version wasn't already uploaded as part of the "versions" collection

if r.IsXref() && versions != nil {
return nil, false,
fmt.Errorf(`Can't update "versions" if "xref" is set`)
}

// If we're processing children, and have a versions collection, process it
if len(versions) > 0 {
plural := "versions"
Expand All @@ -300,7 +317,8 @@ func (g *Group) UpsertResourceWithObject(rType string, id string, vID string, ob

// Process the "meta" sub-object if there
if !IsNil(metaObj) {
_, _, err := r.UpsertMetaWithObject(metaObj, addType, false)
// _, _, err := r.UpsertMetaWithObject(metaObj, addType, false, true)
err := r.ProcessVersionInfo()
if err != nil {
if isNew {
// Needed if doing local func calls to create the Resource
Expand Down
15 changes: 14 additions & 1 deletion registry/httpStuff.go
Original file line number Diff line number Diff line change
Expand Up @@ -1844,7 +1844,8 @@ func HTTPPutPost(info *RequestInfo) error {
}

// Technically, this will always "update" not "insert"
meta, _, err := resource.UpsertMetaWithObject(IncomingObj, addType, true)
meta, _, err := resource.UpsertMetaWithObject(IncomingObj, addType,
true, true)
if err != nil {
info.StatusCode = http.StatusBadRequest
return err
Expand Down Expand Up @@ -1941,6 +1942,10 @@ func HTTPPutPost(info *RequestInfo) error {
// won't process it again, but add it to the reuslts collection
paths = append(paths, v.Path)
delete(objMap, vID)
} else {
if resource.IsXref() {
return fmt.Errorf(`Can't update "versions" if "xref" is set`)
}
}

// Process the remaining versions
Expand Down Expand Up @@ -2152,6 +2157,10 @@ func ProcessSetDefaultVersionIDFlag(info *RequestInfo, resource *Resource, versi
return nil
}

if resource.IsXref() {
return fmt.Errorf(`Can't update "defaultversionid" if "xref" is set`)
}

if info.ResourceModel.GetSetDefaultSticky() == false {
info.StatusCode = http.StatusBadRequest
return fmt.Errorf(`Resource %q doesn't allow setting of `+
Expand Down Expand Up @@ -2345,6 +2354,10 @@ func HTTPDelete(info *RequestInfo) error {
return fmt.Errorf(`DELETE is not allowed on a "meta"`)
}

if resource.IsXref() {
return fmt.Errorf(`Can't delete "versions" if "xref" is set`)
}

if len(info.Parts) == 5 {
// DELETE /GROUPs/gID/RESOURCEs/rID/versions
return HTTPDeleteVersions(info)
Expand Down
2 changes: 2 additions & 0 deletions registry/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,8 @@ func ParseRequest(tx *Tx, w http.ResponseWriter, r *http.Request) (*RequestInfo,
extras: map[string]any{},
}

PanicIf(info.Registry == nil, "No default registry")

if info.Registry != nil && tx.Registry == nil {
tx.Registry = info.Registry
}
Expand Down
6 changes: 6 additions & 0 deletions registry/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -1963,6 +1963,9 @@ func (rm *ResourceModel) GetBaseAttributes() Attributes {
e.NewObject["#resource"] = v
// e.NewObject["#resourceURL"] = nil
delete(e.NewObject, singular)
if !IsNil(v) && e.Type == ENTITY_VERSION {
e.NewObject["#contentid"] = e.DbSID
}
}
return nil
},
Expand Down Expand Up @@ -2028,6 +2031,9 @@ func (rm *ResourceModel) GetBaseAttributes() Attributes {
e.NewObject["#resource"] = v
// e.NewObject["#resourceURL"] = nil
delete(e.NewObject, singular+"base64")
if !IsNil(v) && e.Type == ENTITY_VERSION {
e.NewObject["#contentid"] = e.DbSID
}
return nil
},
},
Expand Down
Loading

0 comments on commit 2c69dca

Please sign in to comment.