Skip to content

Commit 387dda7

Browse files
committed
s/?export/?compact/
Signed-off-by: Doug Davis <[email protected]>
1 parent 54cd004 commit 387dda7

13 files changed

+384
-92
lines changed

cmds/server/loader.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ func LoadDirsSample(reg *registry.Registry) *registry.Registry {
291291

292292
ErrFatalf(reg.Model.Verify())
293293

294-
g, err := reg.AddGroup("dirs", "dir1")
294+
g, err := reg.AddGroup("dirs", "d1")
295295
ErrFatalf(err)
296296
ErrFatalf(g.SetSave("labels.private", "true"))
297297
r, err := g.AddResource("files", "f1", "v1")
@@ -303,10 +303,16 @@ func LoadDirsSample(reg *registry.Registry) *registry.Registry {
303303
ErrFatalf(r.SetSaveMeta("labels.none", ""))
304304
ErrFatalf(r.SetSaveMeta("rext", "a string"))
305305
ErrFatalf(r.SetSaveDefault("vext", "a ver string"))
306-
ErrFatalf(reg.SetSave("resptr", "/dirs/dir1/files/f1/versions/v1"))
306+
ErrFatalf(reg.SetSave("resptr", "/dirs/d1/files/f1/versions/v1"))
307307

308308
_, err = g.AddResource("datas", "d1", "v1")
309309

310+
_, err = g.AddResourceWithObject("files", "fx", "",
311+
map[string]any{
312+
"meta": map[string]any{"xref": "/dirs/d1/files/f1"},
313+
}, true, false)
314+
ErrFatalf(err)
315+
310316
reg.Commit()
311317
return reg
312318
}

registry/capabilities.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ type Capabilities struct {
2020
}
2121

2222
var AllowableFlags = ArrayToLower([]string{
23-
"epoch", "export", "filter", "inline",
23+
"epoch", "compact", "filter", "inline",
2424
"nested", "nodefaultversionid", "nodefaultversionsticky",
2525
"noepoch", "noreadonly", "schema", "setdefaultversionid", "specversion"})
2626

registry/entity.go

+11-5
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,7 @@ func (e *Entity) SetDBProperty(pp *PropPath, val any) error {
601601
PanicIf(e.Registry == nil, "Registry should not be nil")
602602

603603
// #resource is special and is saved in it's own table
604-
// Need to explicitly set #resoure to nil to delete it.
604+
// Need to explicitly set #resource to nil to delete it.
605605
if pp.Len() == 1 && pp.Top() == "#resource" {
606606
if IsNil(val) {
607607
err = Do(e.tx, `DELETE FROM ResourceContents WHERE VersionSID=?`,
@@ -618,6 +618,12 @@ func (e *Entity) SetDBProperty(pp *PropPath, val any) error {
618618
if err != nil {
619619
return err
620620
}
621+
622+
// DUG TODO for fixing xref #resource issue
623+
// Store versionSID as #resource's value, we'll use that
624+
// as the look-up into the ResourceContents table. We can't
625+
// count on the VersionSID because when we're in an xref'd
626+
// entity the VersionSID won't be correct
621627
val = ""
622628
// Fall thru to normal processing so we save a placeholder
623629
// attribute in the resource
@@ -674,7 +680,7 @@ func (e *Entity) SetDBProperty(pp *PropPath, val any) error {
674680

675681
err = DoOneTwo(e.tx, `
676682
REPLACE INTO Props(
677-
RegistrySID, EntitySID, PropName, PropValue, PropType, Export)
683+
RegistrySID, EntitySID, PropName, PropValue, PropType, Compact)
678684
VALUES( ?,?,?,?,?, true )`,
679685
e.Registry.DbSID, e.DbSID, name, dbVal, propType)
680686
}
@@ -1013,7 +1019,7 @@ var OrderedSpecProps = []*Attribute{
10131019

10141020
if e.Type == ENTITY_RESOURCE || e.Type == ENTITY_VERSION {
10151021
meta := info != nil && (info.ShowStructure ||
1016-
info.HasFlag("export") ||
1022+
info.HasFlag("compact") ||
10171023
info.ResourceUID == "" || len(info.Parts) == 5)
10181024
_, rm := e.GetModels()
10191025
if rm.GetHasDocument() == false {
@@ -1048,7 +1054,7 @@ var OrderedSpecProps = []*Attribute{
10481054
}
10491055
if e.Type == ENTITY_RESOURCE || e.Type == ENTITY_VERSION {
10501056
meta := info != nil && (info.ShowStructure ||
1051-
info.HasFlag("export") ||
1057+
info.HasFlag("compact") ||
10521058
info.ResourceUID == "" || len(info.Parts) == 5)
10531059
_, rm := e.GetModels()
10541060
if rm.GetHasDocument() == false {
@@ -1456,7 +1462,7 @@ var OrderedSpecProps = []*Attribute{
14561462
tmp := base + "/" + rPath + "/versions/" + val.(string)
14571463

14581464
meta := info != nil && (info.ShowStructure ||
1459-
info.HasFlag("export") || info.ResourceUID == "")
1465+
info.HasFlag("compact") || info.ResourceUID == "")
14601466
_, rm := e.GetModels()
14611467
if rm.GetHasDocument() == false {
14621468
meta = false

registry/httpStuff.go

+42-21
Original file line numberDiff line numberDiff line change
@@ -362,11 +362,11 @@ func (pw *PageWriter) Done() {
362362

363363
if pw.Info.RootPath == "" {
364364
checked := ""
365-
if pw.Info.HasFlag("export") {
365+
if pw.Info.HasFlag("compact") {
366366
checked = " checked"
367367
}
368-
options += " <div class=export>\n" +
369-
" <input id=export type='checkbox'" + checked + "/>export\n" +
368+
options += " <div class=compact>\n" +
369+
" <input id=compact type='checkbox'" + checked + "/>compact\n" +
370370
" </div>\n"
371371

372372
if options != "" { // Wrapper if any
@@ -405,16 +405,13 @@ func (pw *PageWriter) Done() {
405405
// Process inlines
406406

407407
// check to see if the currently selected inlines are the default
408-
// ones based on any variant of 'export'
408+
// ones based on presence of '/export'
409409
defaultInlines := false
410410
if pw.Info.RootPath == "export" {
411411
defaultInlines = (len(pw.Info.Inlines) == 3 &&
412412
pw.Info.IsInlineSet(NewPPP("capabilities").DB()) &&
413413
pw.Info.IsInlineSet(NewPPP("model").DB()) &&
414414
pw.Info.IsInlineSet(NewPPP("*").DB()))
415-
} else if pw.Info.RootPath == "" && pw.Info.HasFlag("export") {
416-
defaultInlines = (len(pw.Info.Inlines) == 1 &&
417-
pw.Info.IsInlineSet(NewPPP("*").DB()))
418415
}
419416

420417
if pw.Info.RootPath == "" || pw.Info.RootPath == "export" {
@@ -695,7 +692,7 @@ func (pw *PageWriter) Done() {
695692
font-weight: bold ;
696693
margin-left: 3px ;
697694
}
698-
.export {
695+
.compact {
699696
margin-top: 5px ;
700697
font-size: 13px ;
701698
font-family: courier ;
@@ -860,8 +857,8 @@ function apply() {
860857
if (structureswitch) loc += "$structure"
861858
loc += "?ui"
862859
863-
ex = document.getElementById("export")
864-
if (ex != null && ex.checked) loc += "&export"
860+
ex = document.getElementById("compact")
861+
if (ex != null && ex.checked) loc += "&compact"
865862
866863
var filters = document.getElementById("filters").value
867864
var lines = filters.split("\n")
@@ -1131,7 +1128,12 @@ FROM FullTree WHERE RegSID=? AND `
11311128
v, err := readNextEntity(info.tx, results)
11321129

11331130
if v != nil && v.Type == ENTITY_META {
1134-
// Skip the "meta" subobject
1131+
// Skip the "meta" subobject, but first grab the
1132+
// "defaultversionid if we don't already have it,
1133+
// which should only be true in the xref case
1134+
if vID == "" {
1135+
vID = v.GetAsString("defaultversionid")
1136+
}
11351137
continue
11361138
}
11371139

@@ -1282,7 +1284,7 @@ func HTTPGet(info *RequestInfo) error {
12821284
// response body or not (meaning, the hasDoc doc)
12831285
metaInBody := (info.ResourceModel == nil) ||
12841286
(info.ResourceModel.GetHasDocument() == false || info.ShowStructure ||
1285-
info.HasFlag("export") ||
1287+
info.HasFlag("compact") ||
12861288
(len(info.Parts) == 5 && info.Parts[4] == "meta"))
12871289

12881290
// Return the Resource's document
@@ -1316,14 +1318,11 @@ func SerializeQuery(info *RequestInfo, paths []string, what string,
13161318
info.AddInline("capabilities")
13171319
info.AddInline("model")
13181320
}
1319-
if info.RootPath == "" && info.HasFlag("export") && len(info.Inlines) == 0 {
1320-
info.AddInline("*")
1321-
}
13221321

1323-
doExport := info.HasFlag("export") || info.RootPath == "export"
1322+
doCompact := info.HasFlag("compact") || info.RootPath == "export"
13241323

13251324
query, args, err := GenerateQuery(info.Registry, what, paths, filters,
1326-
doExport)
1325+
doCompact)
13271326
results, err := Query(info.tx, query, args...)
13281327
defer results.Close()
13291328

@@ -1345,6 +1344,27 @@ func SerializeQuery(info *RequestInfo, paths []string, what string,
13451344
// Collections will need to print the {}, so don't error for them
13461345
if what != "Coll" {
13471346
if jw.Entity == nil {
1347+
// Special case, if the URL is ../rID/versions/vID?compact then
1348+
// check to see if Resource has xref set, if so then the error
1349+
// is 400, not 404
1350+
if info.VersionUID != "" && info.HasFlag("compact") {
1351+
path := strings.Join(info.Parts[:len(info.Parts)-2], "/")
1352+
path += "/meta"
1353+
entity, err := RawEntityFromPath(info.tx, info.Registry.DbSID,
1354+
path, false)
1355+
if err != nil {
1356+
return err
1357+
}
1358+
1359+
// Assume that if we cant' find the Resource's meta object
1360+
// then the Resource doesn't exist, so a 404 really is the
1361+
// best response in those cases, so skip the 400
1362+
if entity != nil && !IsNil(entity.Object["xref"]) {
1363+
info.StatusCode = http.StatusBadRequest
1364+
return fmt.Errorf("'compact' flag not allowed on xref'd Versions")
1365+
}
1366+
}
1367+
13481368
info.StatusCode = http.StatusNotFound
13491369
return fmt.Errorf("Not found")
13501370
}
@@ -1367,14 +1387,15 @@ func SerializeQuery(info *RequestInfo, paths []string, what string,
13671387
}
13681388
}
13691389

1370-
// Another special case... .../rID/versions?export when rID has xref set
1371-
if jw.Entity == nil && info.HasFlag("export") && len(info.Parts) == 5 && info.Parts[4] == "versions" {
1390+
// GROUPS/gID/RESOURCES/rID/versions
1391+
// Another special case .../rID/versions?compact when rID has xref
1392+
if jw.Entity == nil && info.HasFlag("compact") && len(info.Parts) == 5 && info.Parts[4] == "versions" {
13721393
// Should be our case since "versions" can never be empty except
13731394
// when xref is set. If this is not longer true then we'll need to
13741395
// check this Resource's xref to see if it's set.
13751396
// Can copy the RawEntityFromPath... stuff above
1376-
info.StatusCode = http.StatusNotFound
1377-
return fmt.Errorf("Not found")
1397+
info.StatusCode = http.StatusBadRequest
1398+
return fmt.Errorf("'compact' flag not allowed on xref'd Versions")
13781399
}
13791400

13801401
info.AddHeader("Content-Type", "application/json")

registry/init.sql

+9-9
Original file line numberDiff line numberDiff line change
@@ -240,13 +240,13 @@ CREATE TABLE Props (
240240
PropName VARCHAR(64) NOT NULL,
241241
PropValue VARCHAR($MAX_VARCHAR),
242242
PropType CHAR(64) NOT NULL, # string, boolean, int, ...
243-
Export BOOL NOT NULL, # Should include during export?
243+
Compact BOOL NOT NULL, # Should include during compact?
244244

245-
# non-Export-able attributes are ones that are generated at runtime
245+
# non-compact-able attributes are ones that are generated at runtime
246246
# due to things like showing the Default Version props in the Resource
247247
# or entities/props that materialize due to an xref. Normally a GET
248-
# will show all props, but during /export or ?export we want to exclude
249-
# these non-Export ones. In case where all of the props for an entity
248+
# will show all props, but during /export or ?compact we want to exclude
249+
# these non-compact ones. In case where all of the props for an entity
250250
# are generated, the entire entity should vanish from the serialization.
251251
# e.g. Versions of an xref'd Resource.
252252

@@ -371,7 +371,7 @@ SELECT
371371
P.PropName,
372372
P.PropValue,
373373
P.PropType,
374-
false # Export
374+
false # Compact
375375
FROM xRefSrc2TgtResources AS xR
376376
JOIN Metas AS Ms ON (Ms.ResourceSID=xR.SourceSID)
377377
JOIN Metas AS Mt ON (Mt.ResourceSID=xR.TargetSID)
@@ -399,7 +399,7 @@ UNION SELECT # Find all Version attributes (not meta)
399399
P.PropName,
400400
P.PropValue,
401401
P.PropType,
402-
false # Export
402+
false # Compact
403403
FROM xRefSrc2TgtResources AS xR
404404
JOIN Props AS P ON (
405405
P.EntitySID IN (
@@ -429,7 +429,7 @@ SELECT
429429
p.PropName,
430430
p.PropValue,
431431
p.PropType,
432-
false # Export
432+
false # Compact
433433
FROM EffectiveProps AS p
434434
JOIN EffectiveVersions AS v ON (p.EntitySID=v.SID)
435435
JOIN Metas AS m ON (m.ResourceSID=v.ResourceSID)
@@ -457,7 +457,7 @@ UNION SELECT # Add Resource.isdefault, always 'true'
457457
'isdefault$DB_IN',
458458
'true',
459459
'boolean',
460-
false # Export
460+
false # Compact
461461
FROM Metas AS m ;
462462

463463
CREATE VIEW AllProps AS
@@ -515,7 +515,7 @@ SELECT
515515
PropValue,
516516
PropType,
517517
Abstract,
518-
Export
518+
Compact
519519
FROM Entities
520520
JOIN AllProps ON (AllProps.EntitySID=Entities.eSID)
521521
ORDER by Path, PropName;

registry/jsonWriter.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ func SerializeResourceContents(jw *JsonWriter, e *Entity, info *RequestInfo, ext
275275
singular := rm.Singular
276276

277277
// If the #resource* props aren't there then just exit.
278-
// This will happen when "export" is enabled because the
278+
// This will happen when "export/compact" is enabled because the
279279
// props won't show up in the Resorce but will on the default version
280280
// TODO really should do this check in entity.SerializeProps
281281
if IsNil(jw.Entity.Object["#resourceURL"]) &&

registry/registry.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -636,21 +636,21 @@ func (reg *Registry) UpsertGroupWithObject(gType string, id string, obj Object,
636636
return g, isNew, nil
637637
}
638638

639-
func GenerateQuery(reg *Registry, what string, paths []string, filters [][]*FilterExpr, export bool) (string, []interface{}, error) {
639+
func GenerateQuery(reg *Registry, what string, paths []string, filters [][]*FilterExpr, compact bool) (string, []interface{}, error) {
640640
query := ""
641641
args := []any{}
642642

643643
args = []interface{}{reg.DbSID}
644644
query = `
645645
SELECT
646646
RegSID,Type,Plural,Singular,eSID,UID,PropName,PropValue,PropType,Path,Abstract
647-
FROM FullTree WHERE RegSID=?` // and Export=true
647+
FROM FullTree WHERE RegSID=?` // and Compact=true
648648

649-
// Exclude generated attributes/entityes if export is turned on.
650-
// Meaning, only grab Props that have 'Export' set to 'true'. These
649+
// Exclude generated attributes/entityes if 'compact' is turned on.
650+
// Meaning, only grab Props that have 'Compact' set to 'true'. These
651651
// should be (mainly) just the ones we set explicitly.
652-
if export {
653-
query += ` AND Export=true`
652+
if compact {
653+
query += ` AND Compact=true`
654654
}
655655

656656
// Remove entities that are higher than the GET PATH specified

registry/resource.go

+36
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package registry
22

33
import (
44
"fmt"
5+
"reflect"
56
"sort"
67
"strconv"
78
"strings"
@@ -937,6 +938,41 @@ func (m *Meta) Delete() error {
937938
return DoOne(m.tx, `DELETE FROM Metas WHERE SID=?`, m.DbSID)
938939
}
939940

941+
func (m *Meta) xGet(name string) any {
942+
log.VPrintf(4, "Meta.Get: r(%s).Get(%s)", m.UID, name)
943+
944+
if name == m.Singular+"id" {
945+
return m.Entity.Get(name)
946+
}
947+
948+
// No matching attr, so check if we need to follow xref, and if there
949+
// use that meta
950+
if IsNil(m.NewObject[name]) && IsNil(m.Object[name]) {
951+
_, xRes, err := m.Resource.GetXref()
952+
Must(err)
953+
if xRes != nil {
954+
m, err = xRes.FindMeta(false)
955+
Must(err)
956+
}
957+
}
958+
959+
return m.Entity.Get(name)
960+
}
961+
962+
func (m *Meta) xGetAsString(path string) string {
963+
val := m.Get(path)
964+
if IsNil(val) {
965+
return ""
966+
}
967+
968+
if tmp := reflect.ValueOf(val).Kind(); tmp != reflect.String {
969+
panic(fmt.Sprintf("Not a string - got %T(%v)", val, val))
970+
}
971+
972+
str, _ := val.(string)
973+
return str
974+
}
975+
940976
func (r *Resource) GetVersions() ([]*Version, error) {
941977
list := []*Version{}
942978

0 commit comments

Comments
 (0)