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 10, 2025
1 parent 3db9ea7 commit 066c9d7
Show file tree
Hide file tree
Showing 6 changed files with 588 additions and 33 deletions.
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,6 @@ TODOs:
- support ETag/If-Match
- update epoch/modifiedat of parent when nested entity is added/removed
- ?nested isn't needed to update `model` or `meta` attributes
- test for PUT with "versions" on an existing resource/xref - should igore
the versions
- test clearing xref and setting defver attrs, via res and via meta directly
- test to ensure meta epoch changes as versions are added/removed
- test the timestamps in meta. Should change as versions are added/removed.
- remove entities from cache upon delete
Expand Down
2 changes: 1 addition & 1 deletion registry/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ 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)
_, _, err := r.UpsertMetaWithObject(metaObj, addType, false)
if err != nil {
if isNew {
// Needed if doing local func calls to create the Resource
Expand Down
2 changes: 1 addition & 1 deletion registry/httpStuff.go
Original file line number Diff line number Diff line change
Expand Up @@ -1609,7 +1609,7 @@ func HTTPPutPost(info *RequestInfo) error {
}

// Technically, this will always "update" not "insert"
meta, _, err := resource.UpsertMetaWithObject(IncomingObj, addType)
meta, _, err := resource.UpsertMetaWithObject(IncomingObj, addType, true)
if err != nil {
info.StatusCode = http.StatusBadRequest
return err
Expand Down
68 changes: 42 additions & 26 deletions registry/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,8 +382,13 @@ func (r *Resource) SetDefault(newDefault *Version) error {
return meta.SetSave("defaultversionid", newDefault.UID)
}

// *Meta, isNew, error
func (r *Resource) UpsertMetaWithObject(obj Object, addType AddType) (*Meta, bool, error) {
// returns *Meta, isNew, error
// "createVersion" means we should create a version if there isn't already
// one there. This will only happen when the client talks directly to "meta"
// w/o the surrounding Resource object. AND, for now, we only do it when
// we're removing the 'xref' attr. Other cases, the http layer would have
// already create the Resource and default version for us.
func (r *Resource) UpsertMetaWithObject(obj Object, addType AddType, createVersion bool) (*Meta, bool, error) {
log.VPrintf(3, ">Enter: UpsertMeta(%s,%v)", r.UID, addType)
defer log.VPrintf(3, "<Exit: UpsertMeta")

Expand Down Expand Up @@ -415,10 +420,7 @@ func (r *Resource) UpsertMetaWithObject(obj Object, addType AddType) (*Meta, boo
r.Singular + "id",
"#nextversionid",
"#epoch", // Last used epoch so we can restore it when xref is cleared
// "epoch",
"#createdat",
"#modifiedat"}
//, "defaultversionid"
"#createdat"}

if r.tx.IgnoreDefaultVersionID && !IsNil(obj) {
delete(obj, "defaultversionid")
Expand Down Expand Up @@ -525,6 +527,12 @@ func (r *Resource) UpsertMetaWithObject(obj Object, addType AddType) (*Meta, boo
if targetEpoch > newEpoch {
newEpoch = targetEpoch
}
meta.JustSet("epoch", newEpoch)
meta.JustSet("#epoch", nil)
// We have to fake out the updateFn to think the existing values
// are the # values
meta.EpochSet = false
meta.Object["epoch"] = newEpoch

delete(meta.NewObject, "xref")
if err = meta.JustSet("xref", nil); err != nil {
Expand All @@ -537,24 +545,25 @@ func (r *Resource) UpsertMetaWithObject(obj Object, addType AddType) (*Meta, boo
meta.JustSet("#nextversionid", 1)
}

meta.JustSet("epoch", newEpoch)
meta.JustSet("#epoch", nil)

// We have to fake out the updateFn to think the existing values
// are the # values
meta.Object["epoch"] = newEpoch

meta.EpochSet = false

if IsNil(meta.NewObject["createdat"]) {
meta.JustSet("createdat", meta.Object["#createdat"])
meta.JustSet("#createdat", nil)
meta.Object["createdat"] = meta.Object["#createdat"]
}
if IsNil(meta.NewObject["modifiedat"]) {
meta.JustSet("modifiedat", meta.Object["#modifiedat"])
meta.JustSet("#modifiedat", nil)
meta.Object["modifiedat"] = meta.Object["#modifiedat"]

// if createVersion is true, make sure we have at least one
// version
if createVersion {
vs, err := r.GetVersionIDs()
if err != nil {
return nil, false, err
}
if len(vs) == 0 {
_, _, err = r.UpsertVersionWithObject("", nil, ADD_ADD)
if err != nil {
return nil, false, err
}
}
}

/*
Expand All @@ -566,11 +575,19 @@ func (r *Resource) UpsertMetaWithObject(obj Object, addType AddType) (*Meta, boo
*/
} else {
// Clear all existing attributes except ID
meta.JustSet("#epoch", meta.Object["epoch"])
meta.JustSet("#createdat", meta.Object["createdat"])
oldEpoch := meta.Object["epoch"]
if IsNil(oldEpoch) {
oldEpoch = 0
}
meta.JustSet("#epoch", oldEpoch)

oldCA := meta.Object["createdat"]
if IsNil(oldCA) {
oldCA = meta.tx.CreateTime
}
meta.JustSet("#createdat", oldCA)

// meta.JustSet("createdat", nil)
meta.JustSet("#modifiedat", meta.Object["modifiedat"])
// meta.JustSet("modifiedat", nil)

extraAttrs := []string{}
for k, _ := range meta.NewObject {
Expand Down Expand Up @@ -622,7 +639,7 @@ func (r *Resource) UpsertMetaWithObject(obj Object, addType AddType) (*Meta, boo
// - defaultversionsticky, if there
// - defaultversionid, if defaultversionsticky is set

stickyAny := meta.NewObject["defaultversionsticky"]
stickyAny := meta.Get("defaultversionsticky")
if !IsNil(stickyAny) && stickyAny != true && stickyAny != false {
return nil, false, fmt.Errorf("'defaultversionsticky' must be a " +
"boolean or null")
Expand All @@ -645,7 +662,7 @@ func (r *Resource) UpsertMetaWithObject(obj Object, addType AddType) (*Meta, boo
Must(err)
if defaultVersionID != "" && IsNil(v) {
return nil, false,
fmt.Errorf("Can't find version %q", defaultVersionID)
fmt.Errorf("Version %q not found", defaultVersionID)
}

meta.JustSet(r.Singular+"id", r.UID)
Expand Down Expand Up @@ -752,7 +769,6 @@ func (r *Resource) UpsertVersionWithObject(id string, obj Object, addType AddTyp
return nil, false, err
}
}

// Apply properties
if obj != nil {
// If there's a doc but no "contenttype" value then:
Expand Down
2 changes: 1 addition & 1 deletion tests/http2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5780,7 +5780,7 @@ func TestHTTPNestedResources(t *testing.T) {
}`,
Code: 400,
ResHeaders: []string{},
ResBody: `Can't find version "v2"
ResBody: `Version "v2" not found
`,
})

Expand Down
Loading

0 comments on commit 066c9d7

Please sign in to comment.