-
Notifications
You must be signed in to change notification settings - Fork 68
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add some schema helper methods for SessionPool #312
Changes from 5 commits
a2971b3
77de3e9
232e256
9a29ba0
da23c7c
e14c648
af80f89
d4cd87f
5499e88
4df4a20
bb03a04
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package nebula_go | ||
|
||
import ( | ||
"strings" | ||
) | ||
|
||
type Label struct { | ||
Field string `nebula:"Field"` | ||
Type string `nebula:"Type"` | ||
Null string `nebula:"Null"` | ||
Default string `nebula:"Default"` | ||
Comment string `nebula:"Comment"` | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we can define There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
type LabelFieldSchema struct { | ||
Field string | ||
Type string | ||
Null bool | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IsNull is better for bool var name? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I change it to |
||
} | ||
|
||
type LabelSchema struct { | ||
Name string | ||
Fields []LabelFieldSchema | ||
} | ||
|
||
func (tag LabelSchema) BuildCreateTagQL() string { | ||
q := "CREATE TAG IF NOT EXISTS " + tag.Name + " (" | ||
|
||
fs := []string{} | ||
for _, field := range tag.Fields { | ||
t := field.Type | ||
if t == "" { | ||
t = "string" | ||
} | ||
n := "NULL" | ||
if !field.Null { | ||
n = "NOT NULL" | ||
} | ||
fs = append(fs, field.Field+" "+t+" "+n) | ||
} | ||
|
||
q += strings.Join(fs, ", ") + ");" | ||
|
||
return q | ||
} | ||
|
||
func (tag LabelSchema) BuildDropTagQL() string { | ||
q := "DROP TAG IF EXISTS " + tag.Name + ";" | ||
return q | ||
} | ||
|
||
func (edge LabelSchema) BuildCreateEdgeQL() string { | ||
q := "CREATE EDGE IF NOT EXISTS " + edge.Name + " (" | ||
|
||
fs := []string{} | ||
for _, field := range edge.Fields { | ||
t := field.Type | ||
if t == "" { | ||
t = "string" | ||
} | ||
n := "NULL" | ||
if !field.Null { | ||
n = "NOT NULL" | ||
} | ||
fs = append(fs, field.Field+" "+t+" "+n) | ||
} | ||
|
||
if len(fs) > 0 { | ||
q += strings.Join(fs, ", ") | ||
} | ||
|
||
return q + ");" | ||
} | ||
|
||
func (edge LabelSchema) BuildDropEdgeQL() string { | ||
q := "DROP EDGE IF EXISTS " + edge.Name + ";" | ||
return q | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package nebula_go | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestBuildCreateTagQL(t *testing.T) { | ||
tag := LabelSchema{ | ||
Name: "account", | ||
Fields: []LabelFieldSchema{ | ||
{ | ||
Field: "name", | ||
Null: false, | ||
}, | ||
{ | ||
Field: "email", | ||
Null: true, | ||
}, | ||
{ | ||
Field: "phone", | ||
Null: true, | ||
}, | ||
}, | ||
} | ||
assert.Equal(t, "CREATE TAG IF NOT EXISTS account (name string NOT NULL, email string NULL, phone string NULL);", tag.BuildCreateTagQL()) | ||
assert.Equal(t, "DROP TAG IF EXISTS account;", tag.BuildDropTagQL()) | ||
} | ||
|
||
func TestBuildCreateEdgeQL(t *testing.T) { | ||
edge := LabelSchema{ | ||
Name: "account_email", | ||
Fields: []LabelFieldSchema{ | ||
{ | ||
Field: "email", | ||
Null: false, | ||
}, | ||
}, | ||
} | ||
assert.Equal(t, "CREATE EDGE IF NOT EXISTS account_email (email string NOT NULL);", edge.BuildCreateEdgeQL()) | ||
assert.Equal(t, "DROP EDGE IF EXISTS account_email;", edge.BuildDropEdgeQL()) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please add copyright for new files.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done