Skip to content

Commit 0021ca2

Browse files
Varun-Sethuangary
andauthored
Editor DataModel Refactor (#175)
* editor datamodel refactor * Apply suggestions from code review Spelling Co-authored-by: Gary Sun <garysun0451@gmail.com> * Update Backend/editor/data/jsonConfig.go Spelling Co-authored-by: Gary Sun <garysun0451@gmail.com> * general cleanup of marshaller * cleaned up traversal logic * Update Backend/editor/data/traversal.go Co-authored-by: Gary Sun <garysun0451@gmail.com> * Update Backend/editor/data/traversal.go Co-authored-by: Gary Sun <garysun0451@gmail.com> * accurate logging Co-authored-by: Gary Sun <garysun0451@gmail.com>
1 parent 69284bf commit 0021ca2

25 files changed

+483
-426
lines changed
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

Backend/editor/data/applicator.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package data
2+
3+
// applicator manages the application of request objects to a provided datamodel
4+
5+
import (
6+
"errors"
7+
"reflect"
8+
9+
"cms.csesoc.unsw.edu.au/editor/data/datamodels"
10+
)
11+
12+
// TODO:
13+
// - Add error checking for the paths as we traverse, e.g missing an index when traversing an array (assuming we didn't reach the end)
14+
// - Make sure the item we are adding keeps the validity of the object
15+
16+
// ApplyRequest takes a datamodel (as defined in the datamodels folder) and a request, it then proceeds to apply the request
17+
// to the model, note that this assumes that the operation in the request has been appropriately transformed
18+
func ApplyRequest(model datamodels.DataModel, request Request) error {
19+
_, err := GetOperationTargetSite(model, request.path)
20+
if err != nil {
21+
return err
22+
}
23+
24+
switch editType := request.payload.GetType(); editType {
25+
case "textEdit":
26+
case "keyEdit":
27+
case "arrayEdit":
28+
default:
29+
return errors.New("invalid edit type")
30+
}
31+
32+
return nil
33+
}
34+
35+
// getOperationTargetSite Gets the target object at the end of the path, this is the operation that we need to apply
36+
// our operation to
37+
func GetOperationTargetSite(model datamodels.DataModel, subpaths []string) (reflect.Value, error) {
38+
_, target, err := Traverse(model, subpaths)
39+
if err != nil {
40+
return reflect.Value{}, err
41+
}
42+
43+
return target, nil
44+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Data/Models
2+
3+
Data/Models is where you define the core layout of the data format that you wish to use the collaborative editor with, currently there are only two supported data models, the CMS datamodel (used by the CMS frontend) and the Learning Platform datamodel (used by the learning platform).
4+
5+
## Creating Models
6+
Note that in order to expose your new datamodel you must implement the `IsExposed()` method, this is to prevent passing the wrong models into the CMS functions (so hopefully this can be caught at compile time as opposed to runtime).
7+
8+
## Details
9+
TODO :sunglasses:

Backend/editor/models/component.go renamed to Backend/editor/data/datamodels/cmsmodel/component.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package models
1+
package cmsmodel
22

33
import "reflect"
44

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package cmsmodel
2+
3+
// Document is the main datamodel type of the CMS model, it implements the DataModel interface
4+
type Document struct {
5+
DocumentName string
6+
DocumentId string
7+
Content []Component
8+
}
9+
10+
// IsExposed is the required registration for our type
11+
func (d Document) IsExposed() bool { return true }

Backend/editor/models/image.go renamed to Backend/editor/data/datamodels/cmsmodel/image.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
package models
1+
package cmsmodel
22

3-
import "reflect"
3+
import (
4+
"errors"
5+
"reflect"
6+
)
47

58
// @implements the Component interface
69
type Image struct {
@@ -16,6 +19,10 @@ func (i Image) Get(field string) (reflect.Value, error) {
1619
// Set sets a reflect.Value given a specific field
1720
func (i Image) Set(field string, value reflect.Value) error {
1821
reflectionField := reflect.ValueOf(i).FieldByName(field)
19-
reflectionField.Set(value)
20-
return nil
22+
if reflectionField.IsValid() {
23+
reflectionField.Set(value)
24+
return nil
25+
}
26+
27+
return errors.New("invalid field provided")
2128
}

0 commit comments

Comments
 (0)