Skip to content

Commit

Permalink
Merge pull request #23 from mindstand/cli_patch
Browse files Browse the repository at this point in the history
gogmcli patch
  • Loading branch information
nikitawootten authored Dec 6, 2019
2 parents dfe6bc5 + e7a1c7d commit 1d47505
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 113 deletions.
45 changes: 39 additions & 6 deletions cmd/gogmcli/gen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"fmt"
dsl "github.com/mindstand/go-cypherdsl"
"github.com/mindstand/gogm/cmd/gogmcli/util"
"go/format"
"html/template"
"log"
"os"
Expand Down Expand Up @@ -63,7 +64,7 @@ func Generate(directory string, debug bool) error {
if debug {
log.Printf("parsing go file [%s]\n", filePath)
}
err := parseFile(filePath, &confs, &edges, imps, &packageName)
err := parseFile(filePath, &confs, &edges, imps, &packageName, debug)
if err != nil {
if debug {
log.Printf("failed to parse go file [%s] with error '%s'\n", filePath, err.Error())
Expand All @@ -83,6 +84,10 @@ func Generate(directory string, debug bool) error {
return err
}

if debug {
log.Printf("found [%v] confs and [%v] rels", len(confs), len(edges))
}

var imports []string

for _, imp := range imps {
Expand All @@ -97,13 +102,24 @@ func Generate(directory string, debug bool) error {

relations := make(map[string][]*relConf)

if debug {
log.Println("sorting relationships")
}

// sort out relationships
for _, fields := range confs {
for node, fields := range confs {
if debug {
log.Printf("sorting relationships for node [%s] with [%v] fields", node, len(fields))
}
for _, field := range fields {
if field == nil {
return errors.New("field can not be nil")
}

if debug {
log.Printf("adding relationship [%s] from field [%s]", field.RelationshipName, field.Field)
}

if _, ok := relations[field.RelationshipName]; ok {
relations[field.RelationshipName] = append(relations[field.RelationshipName], field)
} else {
Expand All @@ -112,6 +128,10 @@ func Generate(directory string, debug bool) error {
}
}

if debug {
log.Printf("there are [%v] relations\n", len(relations))
}

// validate relationships (i.e even number)
for name, rel := range relations {
if len(rel)%2 != 0 {
Expand Down Expand Up @@ -140,7 +160,7 @@ func Generate(directory string, debug bool) error {
isSpecialEdge = true
}

err = parseDirection(rel, rels, tplRel, isSpecialEdge)
err = parseDirection(rel, &rels, tplRel, isSpecialEdge)
if err != nil {
return err
}
Expand All @@ -149,6 +169,10 @@ func Generate(directory string, debug bool) error {
return fmt.Errorf("oposite side not found for node [%s]", rel.NodeName)
}

if debug {
log.Printf("adding function to node [%s]", rel.NodeName)
}

if _, ok := funcs[rel.NodeName]; ok {
funcs[rel.NodeName] = append(funcs[rel.NodeName], tplRel)
} else {
Expand Down Expand Up @@ -187,12 +211,20 @@ func Generate(directory string, debug bool) error {
return err
}

// format generated code
formatted, err := format.Source(buf.Bytes())
if err != nil {
return err
}

// create the file
f, err := os.Create(path.Join(directory, "linking.go"))
if err != nil {
return err
}

lenBytes, err := f.Write(buf.Bytes())
// write code to the file
lenBytes, err := f.Write(formatted)
if err != nil {
return err
}
Expand All @@ -201,6 +233,7 @@ func Generate(directory string, debug bool) error {
log.Printf("done after writing [%v] bytes!", lenBytes)
}

// close the buffer
err = f.Close()
if err != nil {
return err
Expand All @@ -212,8 +245,8 @@ func Generate(directory string, debug bool) error {
}

// parseDirection parses gogm struct tags and writes to a holder struct
func parseDirection(rel *relConf, rels []*relConf, tplRel *tplRelConf, isSpecialEdge bool) error {
for _, lookup := range rels {
func parseDirection(rel *relConf, rels *[]*relConf, tplRel *tplRelConf, isSpecialEdge bool) error {
for _, lookup := range *rels {
//check special edge
if rel.Type != lookup.NodeName && !isSpecialEdge {
continue
Expand Down
9 changes: 7 additions & 2 deletions cmd/gogmcli/gen/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type relConf struct {
}

// parses each file using ast looking for nodes to handle
func parseFile(filePath string, confs *map[string][]*relConf, edges *[]string, imports map[string][]string, packageName *string) error {
func parseFile(filePath string, confs *map[string][]*relConf, edges *[]string, imports map[string][]string, packageName *string, debug bool) error {
fset := token.NewFileSet()
node, err := parser.ParseFile(fset, filePath, nil, parser.ParseComments)
if err != nil {
Expand All @@ -52,6 +52,7 @@ func parseFile(filePath string, confs *map[string][]*relConf, edges *[]string, i
*packageName = node.Name.Name
if node.Scope.Objects != nil && len(node.Scope.Objects) != 0 {
for label, config := range node.Scope.Objects {
log.Println("checking ", label)
tSpec, ok := config.Decl.(*ast.TypeSpec)
if !ok {
continue
Expand All @@ -78,6 +79,10 @@ func parseFile(filePath string, confs *map[string][]*relConf, edges *[]string, i
return err
}

if debug {
log.Printf("node [%s] is edge [%v]", label, isEdge)
}

// if its not an edge, parse it as a gogm struct
if !isEdge {
(*confs)[label] = []*relConf{}
Expand Down Expand Up @@ -166,7 +171,7 @@ func parseGogmEdge(node *ast.File, label string) (bool, error) {
}
}
//check if its an edge node
return !GetStartNode || !GetStartNodeType || !SetStartNode || !GetEndNode || !GetEndNodeType || !SetEndNode, nil
return GetStartNode && GetStartNodeType && SetStartNode && GetEndNode && GetEndNodeType && SetEndNode, nil
}

// parseGogmNode generates configuration for GoGM struct
Expand Down
20 changes: 13 additions & 7 deletions cmd/gogmcli/gen/templ.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ package gen

//expect .StructName .OtherStructName .StructField .OtherStructField .StructFieldIsMany .OtherStructFieldIsMany
var linkSpec = `
{{ define "linkSpec" }}
{{ define "linkSpec" }}// LinkTo{{ .OtherStructName }}OnField{{.StructField}} links {{ .StructName }} to {{ .OtherStructName }} on the fields {{ .StructName }}.{{ .StructField }} and {{ .OtherStructName }}.{{ .OtherStructField }}.
// note this uses the special edge {{ .SpecialEdgeType }}
func(l *{{ .StructName }}) LinkTo{{ .OtherStructName }}OnField{{.StructField}}(target *{{ .OtherStructName }}, edge *{{.SpecialEdgeType}}) error {
if target == nil {
return errors.New("start and end can not be nil")
Expand Down Expand Up @@ -71,7 +72,8 @@ func(l *{{ .StructName }}) LinkTo{{ .OtherStructName }}OnField{{.StructField}}(t
`

var singleLink = `
{{ define "linkSingle" }}func(l *{{ .StructName }}) LinkTo{{ .OtherStructName }}OnField{{.StructField}}(target *{{ .OtherStructName }}) error {
{{ define "linkSingle" }}//LinkTo{{ .OtherStructName }}OnField{{.StructField}} links {{ .StructName }} to {{ .OtherStructName }} on the fields {{ .StructName }}.{{ .StructField }} and {{ .OtherStructName }}.{{ .OtherStructField }}
func(l *{{ .StructName }}) LinkTo{{ .OtherStructName }}OnField{{.StructField}}(target *{{ .OtherStructName }}) error {
if target == nil {
return errors.New("start and end can not be nil")
}
Expand All @@ -97,7 +99,7 @@ var singleLink = `
`

var linkMany = `
{{ define "linkMany" }}
{{ define "linkMany" }}// LinkTo{{ .OtherStructName }}OnField{{.StructField}} links {{ .StructName }} to {{ .OtherStructName }} on the fields {{ .StructName }}.{{ .StructField }} and {{ .OtherStructName }}.{{ .OtherStructField }}
func(l *{{ .StructName }}) LinkTo{{ .OtherStructName }}OnField{{.StructField}}(targets ...*{{ .OtherStructName }}) error {
if targets == nil {
return errors.New("start and end can not be nil")
Expand Down Expand Up @@ -127,7 +129,8 @@ func(l *{{ .StructName }}) LinkTo{{ .OtherStructName }}OnField{{.StructField}}(t
`

var unlinkSingle = `
{{ define "unlinkSingle" }}func(l *{{ .StructName }}) UnlinkFrom{{ .OtherStructName }}OnField{{.StructField}}(target *{{ .OtherStructName }}) error {
{{ define "unlinkSingle" }}//UnlinkFrom{{ .OtherStructName }}OnField{{.StructField}} unlinks {{ .StructName }} from {{ .OtherStructName }} on the fields {{ .StructName }}.{{ .StructField }} and {{ .OtherStructName }}.{{ .OtherStructField }}
func(l *{{ .StructName }}) UnlinkFrom{{ .OtherStructName }}OnField{{.StructField}}(target *{{ .OtherStructName }}) error {
if target == nil {
return errors.New("start and end can not be nil")
}
Expand Down Expand Up @@ -163,7 +166,8 @@ var unlinkSingle = `
`

var unlinkMulti = `
{{ define "unlinkMulti" }}func(l *{{ .StructName }}) UnlinkFrom{{ .OtherStructName }}OnField{{.StructField}}(targets ...*{{ .OtherStructName }}) error {
{{ define "unlinkMulti" }}//UnlinkFrom{{ .OtherStructName }}OnField{{.StructField}} unlinks {{ .StructName }} from {{ .OtherStructName }} on the fields {{ .StructName }}.{{ .StructField }} and {{ .OtherStructName }}.{{ .OtherStructField }}
func(l *{{ .StructName }}) UnlinkFrom{{ .OtherStructName }}OnField{{.StructField}}(targets ...*{{ .OtherStructName }}) error {
if targets == nil {
return errors.New("start and end can not be nil")
}
Expand Down Expand Up @@ -202,7 +206,9 @@ var unlinkMulti = `
`

var unlinkSpec = `
{{ define "unlinkSpec" }}func(l *{{ .StructName }}) UnlinkFrom{{ .OtherStructName }}OnField{{.StructField}}(target *{{ .OtherStructName }}) error {
{{ define "unlinkSpec" }}// UnlinkFrom{{ .OtherStructName }}OnField{{.StructField}} unlinks {{ .StructName }} from {{ .OtherStructName }} on the fields {{ .StructName }}.{{ .StructField }} and {{ .OtherStructName }}.{{ .OtherStructField }}.
// also note this uses the special edge {{ .SpecialEdgeType }}
func(l *{{ .StructName }}) UnlinkFrom{{ .OtherStructName }}OnField{{.StructField}}(target *{{ .OtherStructName }}) error {
if target == nil {
return errors.New("start and end can not be nil")
}
Expand Down Expand Up @@ -254,7 +260,7 @@ var unlinkSpec = `
`

var masterTpl = `
{{ define "linkFile" }}// code generated by gogm; DO NOT EDIT
{{ define "linkFile" }}// Code generated by GoGM v1.0.1. DO NOT EDIT
package {{ .PackageName }}
import (
Expand Down
2 changes: 1 addition & 1 deletion cmd/gogmcli/gogm.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func main() {
app := &cli.App{
Name: "gogmcli",
HelpName: "gogmcli",
Version: "1.0.0",
Version: "1.0.1",
Usage: "used for neo4j operations from gogm schema",
Description: "cli for generating and executing migrations with gogm",
EnableBashCompletion: true,
Expand Down
Loading

0 comments on commit 1d47505

Please sign in to comment.