Skip to content

Commit

Permalink
improved graph generation
Browse files Browse the repository at this point in the history
  • Loading branch information
sh0umik committed Oct 10, 2017
1 parent 859102e commit be80863
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 62 deletions.
3 changes: 1 addition & 2 deletions helper/helper.go
@@ -1,6 +1,5 @@
package helper


func IDfilter(ftype string, id string) string {

switch ftype {
Expand All @@ -9,4 +8,4 @@ func IDfilter(ftype string, id string) string {
return id[9:len(id)]
}
return id
}
}
130 changes: 70 additions & 60 deletions jypher.go
Expand Up @@ -3,11 +3,11 @@ package jypher
import (
"fmt"
"github.com/kite-social/jypher/generator"
"github.com/kite-social/jypher/helper"
"github.com/kite-social/jypher/models"
"reflect"
"regexp"
"strings"
"github.com/kite-social/jypher/helper"
)

// Jypher struct
Expand Down Expand Up @@ -42,11 +42,12 @@ func (j *Jypher) BuildCypher() string {
return cypher
}

func (j *Jypher) generateGraph(node string, unmarshal map[string]interface{}, rules models.Rules) map[string]models.Graph {
func (j *Jypher) generateGraph(node string, decodedJSON map[string]interface{}, rules models.Rules) map[string]models.Graph {

nodeName := regexp.MustCompile(`[A-za-z]+`).FindAllString(node, -1)[0]

if rules.Rename != nil {
// apply rename rules before creating a node
nodeName := regexp.MustCompile(`[A-za-z]+`).FindAllString(node, -1)[0]
if name, ok := rules.Rename[nodeName]; ok {
node = regexp.MustCompile(`[A-za-z]+`).ReplaceAllString(node, name.(string))
}
Expand All @@ -55,80 +56,89 @@ func (j *Jypher) generateGraph(node string, unmarshal map[string]interface{}, ru
// match node in the map if not exists then create
// if exists then skip.
// Added to avoid duplicate entry in the model
// Also Skip Meta and Text field
if nodeName != "meta" && nodeName != "text" && nodeName != "extension" {
if _, ok := j.Graph[node]; !ok {

if _, ok := j.Graph[node]; !ok {
j.Graph[node] = models.Graph{}

j.Graph[node] = models.Graph{}
var g models.Graph
g.Nodes.Lebel = node

var g models.Graph
g.Nodes.Lebel = node
g.Edges.Source = j.Master
g.Edges.Target = node

g.Edges.Source = j.Master
g.Edges.Target = node
j.Tree = append(j.Tree, node)
//fmt.Println(parents)

j.Tree = append(j.Tree, node)
//fmt.Println(parents)
if j.Master == node {
// only the master node has the id
g.Nodes.ID = j.ID
}

if j.Master == node {
// only the master node has the id
g.Nodes.ID = j.ID
}
for field, value := range decodedJSON {

for k, v := range unmarshal {
var data map[string]interface{}

var data map[string]interface{}
fieldValue := reflect.ValueOf(value)

t := reflect.ValueOf(v)
switch t.Kind() {
case reflect.String:
pro := map[string]interface{}{
k: v,
}
// if there exists a field called reference then
// there should be existing node that it is referring to
// so add the reference id to node id
if len(pro) > 0 {
if ref, ok := pro["reference"]; ok {
g.Nodes.ID = helper.IDfilter("urn", ref.(string))
switch fieldValue.Kind() {
case reflect.String:
pro := map[string]interface{}{
field: value,
}
}
g.Nodes.Properties = append(g.Nodes.Properties, pro)
// if there exists a field called reference then
// there should be existing node that it is referring to
// so add the reference id to node id
if len(pro) > 0 {
if ref, ok := pro["reference"]; ok {
g.Nodes.ID = helper.IDfilter("urn", ref.(string))
}
}
g.Nodes.Properties = append(g.Nodes.Properties, pro)

j.Graph[node] = g
// If nodeName coding then set code value to ID
if nodeName == "coding" && field == "code" {
g.Nodes.ID = value.(string)
}

case reflect.Map:
data, _ = t.Interface().(map[string]interface{})

j.Master = node
j.generateGraph(k, data, rules)
// loop should reset if we found any object
j.ObjIteration = 0

case reflect.Slice:
slice := reflect.ValueOf(t.Interface())
length := slice.Len()
for i := 0; i < length; i++ {
val := reflect.ValueOf(slice.Index(i).Interface())
switch val.Kind() {
case reflect.String:
// make a saperate node with array of string !! #todo

case reflect.Map:
data, _ = slice.Index(i).Interface().(map[string]interface{})
j.Master = node
j.generateGraph(fmt.Sprintf("%s%d", k, j.ObjIteration), data, rules)
j.ObjIteration++
j.Graph[node] = g

case reflect.Map:
data, _ = fieldValue.Interface().(map[string]interface{})

j.Master = node
j.generateGraph(field, data, rules)
// loop should reset if we found any object
j.ObjIteration = 0

case reflect.Slice:
slice := reflect.ValueOf(fieldValue.Interface())
length := slice.Len()
for i := 0; i < length; i++ {
val := reflect.ValueOf(slice.Index(i).Interface())
switch val.Kind() {
case reflect.String:
// make a saperate node with array of string !! #todo

case reflect.Map:
data, _ = slice.Index(i).Interface().(map[string]interface{})
j.Master = node
j.generateGraph(fmt.Sprintf("%s%d", field, j.ObjIteration), data, rules)
j.ObjIteration++
}
}
}
j.Graph[node] = g
}

j.Graph[node] = g
}

} else {
// If duplicate found skip the parent but process the child #todo
} else {
// If duplicate found skip the parent but process the child #todo

//generateGraph(id, master, unmarshal, graph)
//generateGraph(id, master, unmarshal, graph)
}
}else{
fmt.Sprintf("Skipping %s", nodeName)
}

return j.Graph
Expand Down

0 comments on commit be80863

Please sign in to comment.