Skip to content

Commit c5146dc

Browse files
committed
remove server/client required and just use 'required'
Signed-off-by: Doug Davis <duglin@gmail.com>
1 parent 4a08d83 commit c5146dc

16 files changed

Lines changed: 1194 additions & 1139 deletions

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,3 +142,5 @@ TODOs:
142142
- verify that attrs with default values require "serverrequired" to be true
143143
- test default values - incuding within objects
144144
- add tests for immutable attributes - in particular extensions
145+
- see if we can add RESOURCEid to Versions so we don't need special logic
146+
to exclude them in the code (e.g. in rID's updatefn and validateobj)

registry/entity.go

Lines changed: 109 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -571,10 +571,6 @@ func (e *Entity) ValidateAndSave() error {
571571
return err
572572
}
573573

574-
if err := PrepUpdateEntity(e); err != nil {
575-
return err
576-
}
577-
578574
return e.Save()
579575
}
580576

@@ -874,11 +870,11 @@ func StrTypes(types ...int) string {
874870
// This allows for us to choose the order and define custom logic per prop
875871
var OrderedSpecProps = []*Attribute{
876872
{
877-
Name: "specversion",
878-
Type: STRING,
879-
ReadOnly: true,
880-
Immutable: true,
881-
ServerRequired: true,
873+
Name: "specversion",
874+
Type: STRING,
875+
ReadOnly: true,
876+
Immutable: true,
877+
Required: true,
882878

883879
internals: AttrInternals{
884880
types: StrTypes(ENTITY_REGISTRY),
@@ -897,15 +893,16 @@ var OrderedSpecProps = []*Attribute{
897893
},
898894
},
899895
{
900-
Name: "id",
901-
Type: STRING,
902-
Immutable: true,
903-
ServerRequired: true,
896+
Name: "id",
897+
Type: STRING,
898+
Immutable: true,
899+
Required: true,
904900

905901
internals: AttrInternals{
906-
types: "", // Yes even ENTITY_RESOURCE
907-
dontStore: false,
908-
getFn: nil,
902+
types: "", // Yes even ENTITY_RESOURCE
903+
dontStore: false,
904+
xrefrequired: true,
905+
getFn: nil,
909906
checkFn: func(e *Entity) error {
910907
singular := e.Singular
911908
// PanicIf(singular == "", "singular is '' : %v", e)
@@ -962,10 +959,10 @@ var OrderedSpecProps = []*Attribute{
962959
},
963960
},
964961
{
965-
Name: "versionid",
966-
Type: STRING,
967-
Immutable: true,
968-
ServerRequired: true,
962+
Name: "versionid",
963+
Type: STRING,
964+
Immutable: true,
965+
Required: true,
969966

970967
internals: AttrInternals{
971968
types: StrTypes(ENTITY_VERSION),
@@ -1004,14 +1001,15 @@ var OrderedSpecProps = []*Attribute{
10041001
},
10051002
},
10061003
{
1007-
Name: "self",
1008-
Type: URL,
1009-
ReadOnly: true,
1010-
ServerRequired: true,
1004+
Name: "self",
1005+
Type: URL,
1006+
ReadOnly: true,
1007+
Required: true,
10111008

10121009
internals: AttrInternals{
1013-
types: "", // Yes even ENTITY_RESOURCE
1014-
dontStore: true,
1010+
types: "", // Yes even ENTITY_RESOURCE
1011+
dontStore: true,
1012+
xrefrequired: true,
10151013
getFn: func(e *Entity, info *RequestInfo) any {
10161014
base := ""
10171015
path := e.Path
@@ -1055,11 +1053,12 @@ var OrderedSpecProps = []*Attribute{
10551053
Name: "shortself",
10561054
Type: URL,
10571055
ReadOnly: true,
1058-
ServerRequired: true,
1056+
Required: true,
10591057
10601058
internals: AttrInternals{
10611059
types: "",
10621060
dontStore: true,
1061+
xrefrequired: true,
10631062
getFn: func(e *Entity, info *RequestInfo) any {
10641063
path := e.Path
10651064
base := ""
@@ -1090,16 +1089,17 @@ var OrderedSpecProps = []*Attribute{
10901089
},
10911090
*/
10921091
{
1093-
Name: "xid",
1094-
Type: XID,
1095-
ReadOnly: true,
1096-
ServerRequired: true,
1092+
Name: "xid",
1093+
Type: XID,
1094+
ReadOnly: true,
1095+
Required: true,
10971096

10981097
internals: AttrInternals{
1099-
types: "",
1100-
dontStore: true,
1101-
getFn: nil,
1102-
checkFn: nil,
1098+
types: "",
1099+
dontStore: true,
1100+
xrefrequired: true,
1101+
getFn: nil,
1102+
checkFn: nil,
11031103
},
11041104
},
11051105
{
@@ -1118,9 +1118,9 @@ var OrderedSpecProps = []*Attribute{
11181118
},
11191119
},
11201120
{
1121-
Name: "epoch",
1122-
Type: UINTEGER,
1123-
ServerRequired: true,
1121+
Name: "epoch",
1122+
Type: UINTEGER,
1123+
Required: true,
11241124

11251125
internals: AttrInternals{
11261126
types: StrTypes(ENTITY_REGISTRY, ENTITY_GROUP, ENTITY_META, ENTITY_VERSION),
@@ -1204,11 +1204,11 @@ var OrderedSpecProps = []*Attribute{
12041204
},
12051205
},
12061206
{
1207-
Name: "isdefault",
1208-
Type: BOOLEAN,
1209-
ReadOnly: true,
1210-
ServerRequired: true,
1211-
Default: false,
1207+
Name: "isdefault",
1208+
Type: BOOLEAN,
1209+
ReadOnly: true,
1210+
Required: true,
1211+
Default: false,
12121212

12131213
internals: AttrInternals{
12141214
types: StrTypes(ENTITY_VERSION),
@@ -1263,9 +1263,9 @@ var OrderedSpecProps = []*Attribute{
12631263
},
12641264
},
12651265
{
1266-
Name: "createdat",
1267-
Type: TIMESTAMP,
1268-
ServerRequired: true,
1266+
Name: "createdat",
1267+
Type: TIMESTAMP,
1268+
Required: true,
12691269

12701270
internals: AttrInternals{
12711271
types: StrTypes(ENTITY_REGISTRY, ENTITY_GROUP, ENTITY_META, ENTITY_VERSION),
@@ -1307,9 +1307,9 @@ var OrderedSpecProps = []*Attribute{
13071307
},
13081308
},
13091309
{
1310-
Name: "modifiedat",
1311-
Type: TIMESTAMP,
1312-
ServerRequired: true,
1310+
Name: "modifiedat",
1311+
Type: TIMESTAMP,
1312+
Required: true,
13131313

13141314
internals: AttrInternals{
13151315
types: StrTypes(ENTITY_REGISTRY, ENTITY_GROUP, ENTITY_META, ENTITY_VERSION),
@@ -1348,10 +1348,10 @@ var OrderedSpecProps = []*Attribute{
13481348
},
13491349
},
13501350
{
1351-
Name: "readonly",
1352-
Type: BOOLEAN,
1353-
ServerRequired: true,
1354-
Default: false,
1351+
Name: "readonly",
1352+
Type: BOOLEAN,
1353+
Required: true,
1354+
Default: false,
13551355

13561356
internals: AttrInternals{
13571357
types: StrTypes(ENTITY_META),
@@ -1366,9 +1366,9 @@ var OrderedSpecProps = []*Attribute{
13661366
Type: STRING,
13671367
Enum: []any{"none", "backward", "backward_transitive", "forward",
13681368
"forward_transitive", "full", "full_transitive"},
1369-
Strict: PtrBool(false),
1370-
ServerRequired: true,
1371-
Default: "none",
1369+
Strict: PtrBool(false),
1370+
Required: true,
1371+
Default: "none",
13721372

13731373
internals: AttrInternals{
13741374
types: StrTypes(ENTITY_META),
@@ -1435,7 +1435,7 @@ var OrderedSpecProps = []*Attribute{
14351435
v, ok := e.NewObject[singular+"proxyurl"]
14361436
if ok && !IsNil(v) {
14371437
e.NewObject[singular] = nil
1438-
e.NewObject[singular+"proxyUrl"] = nil
1438+
e.NewObject[singular+"url"] = nil
14391439
e.NewObject["#contentid"] = nil
14401440
}
14411441
return nil
@@ -1467,10 +1467,10 @@ var OrderedSpecProps = []*Attribute{
14671467
},
14681468
},
14691469
{
1470-
Name: "metaurl",
1471-
Type: URL,
1472-
ReadOnly: true,
1473-
ServerRequired: true,
1470+
Name: "metaurl",
1471+
Type: URL,
1472+
ReadOnly: true,
1473+
Required: true,
14741474

14751475
internals: AttrInternals{
14761476
types: StrTypes(ENTITY_RESOURCE),
@@ -1515,7 +1515,7 @@ var OrderedSpecProps = []*Attribute{
15151515
Name: "defaultversionid",
15161516
Type: STRING,
15171517
// ReadOnly: true,
1518-
ServerRequired: true,
1518+
Required: true,
15191519

15201520
internals: AttrInternals{
15211521
types: StrTypes(ENTITY_META),
@@ -1545,10 +1545,10 @@ var OrderedSpecProps = []*Attribute{
15451545
},
15461546
},
15471547
{
1548-
Name: "defaultversionurl",
1549-
Type: URL,
1550-
ReadOnly: true,
1551-
ServerRequired: true,
1548+
Name: "defaultversionurl",
1549+
Type: URL,
1550+
ReadOnly: true,
1551+
Required: true,
15521552

15531553
internals: AttrInternals{
15541554
types: StrTypes(ENTITY_META),
@@ -1622,11 +1622,11 @@ var OrderedSpecProps = []*Attribute{
16221622
},
16231623
},
16241624
{
1625-
Name: "defaultversionsticky",
1626-
Type: BOOLEAN,
1627-
ReadOnly: true,
1628-
ServerRequired: true,
1629-
Default: false,
1625+
Name: "defaultversionsticky",
1626+
Type: BOOLEAN,
1627+
ReadOnly: true,
1628+
Required: true,
1629+
Default: false,
16301630

16311631
internals: AttrInternals{
16321632
types: StrTypes(ENTITY_META),
@@ -2398,37 +2398,51 @@ func (e *Entity) ValidateObject(val any, origAttrs Attributes, path *PropPath) e
23982398
}
23992399
}
24002400

2401-
// We normally skip read-only attrs, but if it has a checkFn
2402-
// then allow for that to be called
2403-
if attr.ReadOnly {
2404-
// Call the attr's checkFn if there
2405-
if attr.internals.checkFn != nil {
2406-
if err := attr.internals.checkFn(e); err != nil {
2407-
return err
2408-
}
2401+
// Call the attr's checkFn if there to make sure any
2402+
// incoming value is ok
2403+
if attr.internals.checkFn != nil {
2404+
if err := attr.internals.checkFn(e); err != nil {
2405+
return err
24092406
}
2407+
}
24102408

2411-
// Now we can skip it
2412-
delete(objKeys, key)
2409+
// Skip read-only attrs
2410+
if attr.ReadOnly {
2411+
delete(objKeys, key) // Remove from to-process list
24132412
continue
24142413
}
24152414

2416-
// Required but not present - note that nil means will be deleted
2417-
if attr.ClientRequired && (!keyPresent || IsNil(val)) {
2418-
return fmt.Errorf("Required property %q is missing",
2419-
path.P(key).UI())
2420-
}
2415+
// If this attr has a func to update its value, call it
2416+
if attr.internals.updateFn != nil {
2417+
if err := attr.internals.updateFn(e); err != nil {
2418+
return err
2419+
}
24212420

2422-
// Not ClientRequired && not there (or being deleted)
2423-
if !attr.ClientRequired && (!keyPresent || IsNil(val)) {
2424-
delete(objKeys, key)
2425-
continue
2421+
// grab value in case it changed
2422+
val, keyPresent = newObj[key]
24262423
}
24272424

2428-
// Call the attr's checkFn if there - for more refined checks
2429-
if attr.internals.checkFn != nil {
2430-
if err := attr.internals.checkFn(e); err != nil {
2431-
return err
2425+
// Required but not present - note that nil means will be deleted
2426+
if attr.Required && (!keyPresent || IsNil(val)) {
2427+
flagit := true // Assume we'll err
2428+
2429+
// Most "meta" attribute aren't actually required when xref
2430+
// is set, so only flag the ones w/o 'xrefrequired=true'
2431+
if e.Type == ENTITY_META && e.GetAsString("xref") != "" &&
2432+
!attr.internals.xrefrequired {
2433+
flagit = false
2434+
}
2435+
2436+
// Version.RESOURCEid MUST be missing, so don't flag it
2437+
// All other entities need that attribute though
2438+
if path.Len() == 0 && e.Type == ENTITY_VERSION &&
2439+
key == e.GetResourceSingular()+"id" {
2440+
flagit = false
2441+
}
2442+
2443+
if flagit {
2444+
return fmt.Errorf("Required property %q is missing",
2445+
path.P(key).UI())
24322446
}
24332447
}
24342448

@@ -2440,7 +2454,7 @@ func (e *Entity) ValidateObject(val any, origAttrs Attributes, path *PropPath) e
24402454
}
24412455
}
24422456

2443-
// Everything is good, so remove it
2457+
// Everything is good, so remove it from to-process list
24442458
delete(objKeys, key)
24452459
}
24462460
}

0 commit comments

Comments
 (0)