diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 5894543..210f137 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -39,6 +39,16 @@ jobs: curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh dep ensure fi + - name: Setup gogmcli test + run: | + export ROOTDIR=`pwd` + rm testing_/linking.go + cd cmd/gogmcli + go build . + mv gogmcli $ROOTDIR/testing_/gogmcli + cd $ROOTDIR/testing_ + ./gogmcli gen . + cd $ROOTDIR - name: Build run: go build -v . - name: Run Unit Tests diff --git a/cmd/gogmcli/gen/parse.go b/cmd/gogmcli/gen/parse.go index 5bfb24a..835b855 100644 --- a/cmd/gogmcli/gen/parse.go +++ b/cmd/gogmcli/gen/parse.go @@ -23,6 +23,7 @@ import ( "bytes" "errors" go_cypherdsl "github.com/mindstand/go-cypherdsl" + "github.com/mindstand/gogm/cmd/gogmcli/util" "go/ast" "go/parser" "go/printer" @@ -185,7 +186,7 @@ func parseGogmNode(strType *ast.StructType, confs *map[string][]*relConf, label if !strings.Contains(part, "gogm") { continue } - part = strings.Replace(strings.Replace(part, "`gogm:", "", -1), "\"", "", -1) + part = util.RemoveFromString(part, "gogm:", "\"", "`") if strings.Contains(part, "relationship") && strings.Contains(part, "direction") { gogmParts := strings.Split(part, ";") @@ -193,7 +194,7 @@ func parseGogmNode(strType *ast.StructType, confs *map[string][]*relConf, label var relName string for _, p := range gogmParts { if strings.Contains(p, "direction") { - str := strings.ToLower(strings.Replace(strings.Replace(strings.Replace(p, "direction=", "", -1), "\"", "", -1), "`", "", -1)) + str := util.RemoveFromString(p, "direction=", "\"") switch str { case "incoming": dir = go_cypherdsl.DirectionIncoming @@ -212,7 +213,7 @@ func parseGogmNode(strType *ast.StructType, confs *map[string][]*relConf, label continue fieldLoop } } else if strings.Contains(part, "relationship") { - relName = strings.ToLower(strings.Replace(strings.Replace(p, "relationship=", "", -1), "\"", "", -1)) + relName = strings.ToLower(util.RemoveFromString(p, "relationship=", "\"", "`")) } } diff --git a/cmd/gogmcli/util/util.go b/cmd/gogmcli/util/util.go index 051158f..3fe2590 100644 --- a/cmd/gogmcli/util/util.go +++ b/cmd/gogmcli/util/util.go @@ -19,6 +19,8 @@ package util +import "strings" + // RemoveDuplicates removes duplicates from string slice func RemoveDuplicates(s []string) []string { if s == nil { @@ -38,6 +40,14 @@ func RemoveDuplicates(s []string) []string { return s[:j] } +func RemoveFromString(original string, removals ...string) string { + for _, r := range removals { + original = strings.Replace(original, r, "", -1) + } + + return original +} + func StringSliceContains(s []string, e string) bool { for _, a := range s { if a == e { diff --git a/testing_/linking.go b/testing_/linking.go index c6ca06a..770715f 100644 --- a/testing_/linking.go +++ b/testing_/linking.go @@ -5,6 +5,70 @@ import ( "errors" ) +// LinkToExampleObject2OnFieldSpecial links ExampleObject to ExampleObject2 on the fields ExampleObject.Special and ExampleObject2.Special. +// note this uses the special edge SpecialEdge +func (l *ExampleObject) LinkToExampleObject2OnFieldSpecial(target *ExampleObject2, edge *SpecialEdge) error { + if target == nil { + return errors.New("start and end can not be nil") + } + + if edge == nil { + return errors.New("edge can not be nil") + } + + err := edge.SetStartNode(l) + if err != nil { + return err + } + + err = edge.SetEndNode(target) + if err != nil { + return err + } + + l.Special = edge + + if target.Special == nil { + target.Special = make([]*SpecialEdge, 1, 1) + target.Special[0] = edge + } else { + target.Special = append(target.Special, edge) + } + + return nil +} + +// UnlinkFromExampleObject2OnFieldSpecial unlinks ExampleObject from ExampleObject2 on the fields ExampleObject.Special and ExampleObject2.Special. +// also note this uses the special edge SpecialEdge +func (l *ExampleObject) UnlinkFromExampleObject2OnFieldSpecial(target *ExampleObject2) error { + if target == nil { + return errors.New("start and end can not be nil") + } + + l.Special = nil + + if target.Special != nil { + for i, unlinkTarget := range target.Special { + + obj := unlinkTarget.GetStartNode() + + checkObj, ok := obj.(*ExampleObject) + if !ok { + return errors.New("unable to cast unlinkTarget to [ExampleObject]") + } + if checkObj.UUID == l.UUID { + a := &target.Special + (*a)[i] = (*a)[len(*a)-1] + (*a)[len(*a)-1] = nil + *a = (*a)[:len(*a)-1] + break + } + } + } + + return nil +} + // LinkToExampleObjectOnFieldChildren links ExampleObject to ExampleObject on the fields ExampleObject.Children and ExampleObject.Parents func (l *ExampleObject) LinkToExampleObjectOnFieldChildren(targets ...*ExampleObject) error { if targets == nil { @@ -93,9 +157,9 @@ func (l *ExampleObject) UnlinkFromExampleObjectOnFieldParents(target *ExampleObj return nil } -// LinkToExampleObject2OnFieldSpecial links ExampleObject to ExampleObject2 on the fields ExampleObject.Special and ExampleObject2.Special. +// LinkToExampleObjectOnFieldSpecial links ExampleObject2 to ExampleObject on the fields ExampleObject2.Special and ExampleObject.Special. // note this uses the special edge SpecialEdge -func (l *ExampleObject) LinkToExampleObject2OnFieldSpecial(target *ExampleObject2, edge *SpecialEdge) error { +func (l *ExampleObject2) LinkToExampleObjectOnFieldSpecial(target *ExampleObject, edge *SpecialEdge) error { if target == nil { return errors.New("start and end can not be nil") } @@ -104,48 +168,46 @@ func (l *ExampleObject) LinkToExampleObject2OnFieldSpecial(target *ExampleObject return errors.New("edge can not be nil") } - err := edge.SetStartNode(l) + err := edge.SetStartNode(target) if err != nil { return err } - err = edge.SetEndNode(target) + err = edge.SetEndNode(l) if err != nil { return err } - l.Special = edge - - if target.Special == nil { - target.Special = make([]*SpecialEdge, 1, 1) - target.Special[0] = edge + if l.Special == nil { + l.Special = make([]*SpecialEdge, 1, 1) + l.Special[0] = edge } else { - target.Special = append(target.Special, edge) + l.Special = append(l.Special, edge) } + target.Special = edge + return nil } -// UnlinkFromExampleObject2OnFieldSpecial unlinks ExampleObject from ExampleObject2 on the fields ExampleObject.Special and ExampleObject2.Special. +// UnlinkFromExampleObjectOnFieldSpecial unlinks ExampleObject2 from ExampleObject on the fields ExampleObject2.Special and ExampleObject.Special. // also note this uses the special edge SpecialEdge -func (l *ExampleObject) UnlinkFromExampleObject2OnFieldSpecial(target *ExampleObject2) error { +func (l *ExampleObject2) UnlinkFromExampleObjectOnFieldSpecial(target *ExampleObject) error { if target == nil { return errors.New("start and end can not be nil") } - l.Special = nil - - if target.Special != nil { - for i, unlinkTarget := range target.Special { + if l.Special != nil { + for i, unlinkTarget := range l.Special { - obj := unlinkTarget.GetStartNode() + obj := unlinkTarget.GetEndNode() checkObj, ok := obj.(*ExampleObject) if !ok { return errors.New("unable to cast unlinkTarget to [ExampleObject]") } - if checkObj.UUID == l.UUID { - a := &target.Special + if checkObj.UUID == target.UUID { + a := &l.Special (*a)[i] = (*a)[len(*a)-1] (*a)[len(*a)-1] = nil *a = (*a)[:len(*a)-1] @@ -154,6 +216,8 @@ func (l *ExampleObject) UnlinkFromExampleObject2OnFieldSpecial(target *ExampleOb } } + target.Special = nil + return nil } @@ -244,67 +308,3 @@ func (l *ExampleObject2) UnlinkFromExampleObject2OnFieldParents2(target *Example return nil } - -// LinkToExampleObjectOnFieldSpecial links ExampleObject2 to ExampleObject on the fields ExampleObject2.Special and ExampleObject.Special. -// note this uses the special edge SpecialEdge -func (l *ExampleObject2) LinkToExampleObjectOnFieldSpecial(target *ExampleObject, edge *SpecialEdge) error { - if target == nil { - return errors.New("start and end can not be nil") - } - - if edge == nil { - return errors.New("edge can not be nil") - } - - err := edge.SetStartNode(target) - if err != nil { - return err - } - - err = edge.SetEndNode(l) - if err != nil { - return err - } - - if l.Special == nil { - l.Special = make([]*SpecialEdge, 1, 1) - l.Special[0] = edge - } else { - l.Special = append(l.Special, edge) - } - - target.Special = edge - - return nil -} - -// UnlinkFromExampleObjectOnFieldSpecial unlinks ExampleObject2 from ExampleObject on the fields ExampleObject2.Special and ExampleObject.Special. -// also note this uses the special edge SpecialEdge -func (l *ExampleObject2) UnlinkFromExampleObjectOnFieldSpecial(target *ExampleObject) error { - if target == nil { - return errors.New("start and end can not be nil") - } - - if l.Special != nil { - for i, unlinkTarget := range l.Special { - - obj := unlinkTarget.GetEndNode() - - checkObj, ok := obj.(*ExampleObject) - if !ok { - return errors.New("unable to cast unlinkTarget to [ExampleObject]") - } - if checkObj.UUID == target.UUID { - a := &l.Special - (*a)[i] = (*a)[len(*a)-1] - (*a)[len(*a)-1] = nil - *a = (*a)[:len(*a)-1] - break - } - } - } - - target.Special = nil - - return nil -} diff --git a/testing_/test_obj.go b/testing_/test_obj.go index 4106462..f7999b2 100644 --- a/testing_/test_obj.go +++ b/testing_/test_obj.go @@ -24,7 +24,7 @@ import "github.com/mindstand/gogm" type ExampleObject struct { gogm.BaseNode - Children []*ExampleObject `gogm:"direction=incoming;relationship=test" json:"children"` + Children []*ExampleObject `json:"children" gogm:"direction=incoming;relationship=test"` Parents *ExampleObject `gogm:"direction=outgoing;relationship=test" json:"parents"` Special *SpecialEdge `gogm:"direction=incoming;relationship=special" json:"special"` }