Skip to content

Commit 9a989b3

Browse files
committed
support modelsource & some more clean-up/tests
Signed-off-by: Doug Davis <[email protected]>
1 parent abbec09 commit 9a989b3

35 files changed

+2772
-281
lines changed

Makefile

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ utest: .utest
5050
@touch .utest
5151

5252
test: .test .testimages
53-
.test: .cmds */*test.go
53+
.test: sharedfiles .cmds */*test.go
5454
@make mysql waitformysql
5555
@echo
5656
@echo "# Testing"
@@ -70,12 +70,18 @@ unittest:
7070
@echo go test -failfast ./registry
7171
@$(GO_TEST) ./registry
7272

73-
xrserver: cmds/xrserver/* registry/* common/*
73+
sharedfiles: registry/shared* cmds/xr/xrlib/shared*
74+
registry/shared* cmds/xr/xrlib/shared*: common/shared*
7475
@echo
75-
@echo "# Building xrserver"
7676
@echo "# Copying shared files"
7777
@sed "s/XXX/registry/g" common/shared_entities > registry/shared_entities.go
78+
@sed "s/XXX/xrlib/g" common/shared_entities > cmds/xr/xrlib/shared_entities.go
7879
@sed "s/XXX/registry/g" common/shared_model > registry/shared_model.go
80+
@sed "s/XXX/xrlib/g" common/shared_model > cmds/xr/xrlib/shared_model.go
81+
82+
xrserver: sharedfiles cmds/xrserver/* registry/* common/*
83+
@echo
84+
@echo "# Building xrserver"
7985
@misc/errOutput -"go build -o $@ cmds/xrserver/*.go" \
8086
go build $(BUILDFLAGS) -o $@ cmds/xrserver/*.go
8187

@@ -89,12 +95,9 @@ xrserver-all: .xrserver-all
8995
GOOS=darwin GOARCH=arm64 go build $(STATIC) -o xrserver.mac.arm64 cmds/xr/*.go
9096
@touch .xrserver-all
9197

92-
xr: cmds/xr/* common/*
98+
xr: sharedfiles cmds/xr/* common/*
9399
@echo
94100
@echo "# Building xr (cli)"
95-
@echo "# Copying shared files"
96-
@sed "s/XXX/xrlib/g" common/shared_entities > cmds/xr/xrlib/shared_entities.go
97-
@sed "s/XXX/xrlib/g" common/shared_model > cmds/xr/xrlib/shared_model.go
98101
@misc/errOutput -"go build -o $@ cmds/xr/*.go" \
99102
go build $(BUILDFLAGS) -o $@ cmds/xr/*.go
100103

@@ -266,7 +269,7 @@ testdev: devimage
266269
clean:
267270
@echo
268271
@echo "# Cleaning"
269-
@rm -f cmds/xr/xrl/shared_*.go registry/shared_.go
272+
@rm -f cmds/xr/xrlib/shared_*.go registry/shared_*.go
270273
@rm -f cpu.prof mem.prof
271274
@rm -f xrserver xrserver.linux* xrserver.mac* xrserver.windows*
272275
@rm -f xr xr.linux* xr.mac* xr.windows.*

cmds/xr/model.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ func modelUpdateFunc(cmd *cobra.Command, args []string) {
241241
Error("Missing model data")
242242
}
243243

244-
_, err = reg.HttpDo("PUT", "/model", []byte(buf))
244+
_, err = reg.HttpDo("PUT", "/modelsource", []byte(buf))
245245
Error(err)
246246
Verbose("Model updated")
247247
}
@@ -415,7 +415,7 @@ func modelGroupCreateFunc(cmd *cobra.Command, args []string) {
415415
reg, err := xrlib.GetRegistry(Server)
416416
Error(err)
417417

418-
model, err := reg.GetModel()
418+
model, err := reg.GetModelSource()
419419
Error(err)
420420
verMsg := ""
421421
for _, arg := range args {
@@ -464,7 +464,7 @@ func modelGroupCreateFunc(cmd *cobra.Command, args []string) {
464464

465465
buf, err := json.MarshalIndent(model, "", " ")
466466
Error(err)
467-
_, err = reg.HttpDo("PUT", "/model", buf)
467+
_, err = reg.HttpDo("PUT", "/modelsource", buf)
468468
Error(err)
469469
Verbose(verMsg)
470470
}
@@ -483,7 +483,7 @@ func modelGroupDeleteFunc(cmd *cobra.Command, args []string) {
483483
reg, err := xrlib.GetRegistry(Server)
484484
Error(err)
485485

486-
model, err := reg.GetModel()
486+
model, err := reg.GetModelSource()
487487
Error(err)
488488
verMsg := ""
489489
for _, arg := range args {
@@ -509,7 +509,7 @@ func modelGroupDeleteFunc(cmd *cobra.Command, args []string) {
509509

510510
buf, err := json.MarshalIndent(model, "", " ")
511511
Error(err)
512-
_, err = reg.HttpDo("PUT", "/model", buf)
512+
_, err = reg.HttpDo("PUT", "/modelsource", buf)
513513
Error(err)
514514
Verbose(verMsg)
515515
}
@@ -531,7 +531,7 @@ func modelResourceCreateFunc(cmd *cobra.Command, args []string) {
531531
reg, err := xrlib.GetRegistry(Server)
532532
Error(err)
533533

534-
model, err := reg.GetModel()
534+
model, err := reg.GetModelSource()
535535
Error(err)
536536
gm := model.FindGroupModel(group)
537537
if gm == nil {
@@ -584,7 +584,7 @@ func modelResourceCreateFunc(cmd *cobra.Command, args []string) {
584584

585585
buf, err := json.MarshalIndent(model, "", " ")
586586
Error(err)
587-
_, err = reg.HttpDo("PUT", "/model", buf)
587+
_, err = reg.HttpDo("PUT", "/modelsource", buf)
588588
Error(err)
589589
Verbose(verMsg)
590590
}
@@ -608,7 +608,7 @@ func modelResourceDeleteFunc(cmd *cobra.Command, args []string) {
608608
reg, err := xrlib.GetRegistry(Server)
609609
Error(err)
610610

611-
model, err := reg.GetModel()
611+
model, err := reg.GetModelSource()
612612
Error(err)
613613
gm := model.FindGroupModel(group)
614614
if gm == nil {
@@ -639,7 +639,7 @@ func modelResourceDeleteFunc(cmd *cobra.Command, args []string) {
639639

640640
buf, err := json.MarshalIndent(model, "", " ")
641641
Error(err)
642-
_, err = reg.HttpDo("PUT", "/model", buf)
642+
_, err = reg.HttpDo("PUT", "/modelsource", buf)
643643
Error(err)
644644
Verbose(verMsg)
645645
}

cmds/xr/xrlib/registry.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,20 @@ func (reg *Registry) RefreshModel() error {
214214
err, string(res.Body))
215215
}
216216
reg.Model.SetPointers()
217+
218+
res, err = reg.HttpDo("GET", "/modelsource", nil)
219+
if err != nil {
220+
return err
221+
}
222+
223+
srcModel := Model{}
224+
225+
if err := json.Unmarshal(res.Body, &srcModel); err != nil {
226+
return fmt.Errorf("Unable to parse registry modelsource: %s\n%s",
227+
err, string(res.Body))
228+
}
229+
reg.Model.Source = string(res.Body)
230+
217231
return nil
218232
}
219233

@@ -254,6 +268,23 @@ func (reg *Registry) GetModel() (*Model, error) {
254268
return reg.Model, nil
255269
}
256270

271+
func (reg *Registry) GetModelSource() (*Model, error) {
272+
if reg.Model == nil {
273+
err := reg.RefreshModel()
274+
if err != nil {
275+
return nil, err
276+
}
277+
}
278+
tmpModel := Model{}
279+
if reg.Model.Source != "" {
280+
err := Unmarshal([]byte(reg.Model.Source), &tmpModel)
281+
if err != nil {
282+
return nil, err
283+
}
284+
}
285+
return &tmpModel, nil
286+
}
287+
257288
func (reg *Registry) GetCapabilities() (*Capabilities, error) {
258289
if reg.Capabilities == nil {
259290
err := reg.RefreshCapabilities()

cmds/xr/xrlib/shared_entities.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,23 @@ var OrderedSpecProps = []*Attribute{
267267
},
268268
},
269269
{
270-
Name: "model",
270+
Name: "model",
271+
Type: OBJECT,
272+
ReadOnly: true,
273+
Attributes: Attributes{
274+
"*": &Attribute{
275+
Name: "*",
276+
Type: ANY,
277+
},
278+
},
279+
280+
internals: &AttrInternals{
281+
types: StrTypes(ENTITY_REGISTRY),
282+
dontStore: true,
283+
},
284+
},
285+
{
286+
Name: "modelsource",
271287
Type: OBJECT,
272288
Attributes: Attributes{
273289
"*": &Attribute{

cmds/xr/xrlib/shared_model.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,15 @@ func IsValidID(id string) error {
6060

6161
type Model struct {
6262
Registry *Registry `json:"-"`
63+
Source string `json:"source,omitempty"`
6364
Labels map[string]string `json:"labels,omitempty"`
6465
Attributes Attributes `json:"attributes,omitempty"`
6566
Groups map[string]*GroupModel `json:"groups,omitempty"` // Plural
6667

6768
propsOrdered []*Attribute
6869
propsMap map[string]*Attribute // Attrs+calculated attrs
6970
changed bool
70-
Stuff map[string]any // random stuff to pass around
71+
Stuff map[string]any `json:"-"` // random stuff to pass around
7172
}
7273

7374
type Attributes map[string]*Attribute // AttrName->Attr

cmds/xrserver/loader.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -547,10 +547,22 @@ func LoadDocStore(reg *registry.Registry) *registry.Registry {
547547
}
548548

549549
Verbose("Loading: /reg-%s", reg.UID)
550-
gm, _ := reg.Model.AddGroupModel("documents", "document")
551-
gm.AddResourceModel("formats", "format", 0, true, true, true)
552-
553-
ErrFatalf(reg.Model.VerifyAndSave())
550+
// Use JSON for this model so that "modelsource" has something in it
551+
ErrFatalf(reg.Model.ApplyNewModelFromJSON([]byte(`{
552+
"groups": {
553+
"documents": {
554+
"singular": "document",
555+
"resources": {
556+
"formats": {
557+
"singular": "format"
558+
}
559+
}
560+
}
561+
}
562+
}
563+
`)))
564+
565+
// ErrFatalf(reg.Model.VerifyAndSave())
554566

555567
g, _ := reg.AddGroup("documents", "mydoc1")
556568
g.SetSave("labels.group", "g1")

common/capabilities.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ type Offered struct {
4545
}
4646

4747
var AllowableAPIs = ArrayToLower([]string{
48-
"/capabilities", "/export", "/model"})
48+
"/capabilities", "/export", "/model", "/modelsource"})
4949

5050
var AllowableFlags = ArrayToLower([]string{
5151
"collections", "doc", "epoch", "filter", "inline",

0 commit comments

Comments
 (0)