Skip to content
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

feat: change api structure #19

Merged
merged 2 commits into from
Apr 23, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
149 changes: 96 additions & 53 deletions service/dao/dao.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import (
"errors"
"log"

nebula "github.com/vesoft-inc/nebula-go"
nebulaType "github.com/vesoft-inc/nebula-go/nebula"
"nebula-http-gateway/service/pool"
common "nebula-http-gateway/utils"

nebula "github.com/vesoft-inc/nebula-go"
nebulaType "github.com/vesoft-inc/nebula-go/nebula"
)

type ExecuteResult struct {
Expand Down Expand Up @@ -160,7 +161,7 @@ func getPathInfo(valWarp *nebula.ValueWrapper, data map[string]common.Any) (map[
return data, nil
}

func getListInfo(valWarp *nebula.ValueWrapper, data []common.Any, listType string) ([]common.Any, error) {
func getListInfo(valWarp *nebula.ValueWrapper, listType string, _verticesParsedList []common.Any, _edgesParsedList []common.Any, _pathsParsedList []common.Any) ([]common.Any, []common.Any, []common.Any, error) {
var valueList []nebula.ValueWrapper
var err error
if listType == "list" {
Expand All @@ -169,76 +170,106 @@ func getListInfo(valWarp *nebula.ValueWrapper, data []common.Any, listType strin
valueList, err = valWarp.AsDedupList()
}
if err != nil {
return nil, err
return _verticesParsedList, _edgesParsedList, _pathsParsedList, err
}
for _, v := range valueList {
var props = make(map[string]common.Any)
vType := v.GetType()
props["type"] = vType
if vType == "vertex" {
props, err = getVertexInfo(&v, props)
if err == nil {
_verticesParsedList = append(_verticesParsedList, props)
} else {
return _verticesParsedList, _edgesParsedList, _pathsParsedList, err
}
} else if vType == "edge" {
props, err = getEdgeInfo(&v, props)
if err == nil {
_edgesParsedList = append(_edgesParsedList, props)
} else {
return _verticesParsedList, _edgesParsedList, _pathsParsedList, err
}
} else if vType == "path" {
props, err = getPathInfo(&v, props)
if err == nil {
_pathsParsedList = append(_pathsParsedList, props)
} else {
return _verticesParsedList, _edgesParsedList, _pathsParsedList, err
}
} else if vType == "list" {
var items = make([]common.Any, 0)
items, err = getListInfo(&v, items, "list")
props["items"] = items
_verticesParsedList, _edgesParsedList, _pathsParsedList, err = getListInfo(&v, "list", _verticesParsedList, _edgesParsedList, _pathsParsedList)
if err != nil {
return _verticesParsedList, _edgesParsedList, _pathsParsedList, err
}
} else if vType == "map" {
var items = make(map[string]common.Any)
items, err = getMapInfo(&v, items)
props["items"] = items
_verticesParsedList, _edgesParsedList, _pathsParsedList, err = getMapInfo(&v, _verticesParsedList, _edgesParsedList, _pathsParsedList)
if err != nil {
return _verticesParsedList, _edgesParsedList, _pathsParsedList, err
}
} else if vType == "set" {
var items = make([]common.Any, 0)
items, err = getListInfo(&v, items, "set")
props["items"] = items
} else {
basicVal, err := getBasicValue(&v)
_verticesParsedList, _edgesParsedList, _pathsParsedList, err = getListInfo(&v, "set", _verticesParsedList, _edgesParsedList, _pathsParsedList)
if err != nil {
return data, err
return _verticesParsedList, _edgesParsedList, _pathsParsedList, err
}
props["value"] = basicVal
} else {
// no need to parse basic value now
}
data = append(data, props)
}
return data, nil
return _verticesParsedList, _edgesParsedList, _pathsParsedList, nil
}

func getMapInfo(valWarp *nebula.ValueWrapper, data map[string]common.Any) (map[string]common.Any, error) {
func getMapInfo(valWarp *nebula.ValueWrapper, _verticesParsedList []common.Any, _edgesParsedList []common.Any, _pathsParsedList []common.Any) ([]common.Any, []common.Any, []common.Any, error) {
valueMap, err := valWarp.AsMap()
if err != nil {
return nil, err
return _verticesParsedList, _edgesParsedList, _pathsParsedList, err
}
for k, v := range valueMap {
for _, v := range valueMap {
vType := v.GetType()
if vType == "vertex" {
var _props map[string]common.Any
_props, err = getVertexInfo(&v, _props)
data[k] = _props
if err == nil {
_verticesParsedList = append(_verticesParsedList, _props)
} else {
return _verticesParsedList, _edgesParsedList, _pathsParsedList, err
}
} else if vType == "edge" {
var _props map[string]common.Any
_props, err = getEdgeInfo(&v, _props)
data[k] = _props
if err == nil {
_edgesParsedList = append(_edgesParsedList, _props)
} else {
return _verticesParsedList, _edgesParsedList, _pathsParsedList, err
}
} else if vType == "path" {
var _props map[string]common.Any
_props, err = getPathInfo(&v, _props)
if err == nil {
_pathsParsedList = append(_pathsParsedList, _props)
} else {
return _verticesParsedList, _edgesParsedList, _pathsParsedList, err
}
} else if vType == "list" {
var items = make([]common.Any, 0)
items, err = getListInfo(&v, items, "list")
data[k] = items
_verticesParsedList, _edgesParsedList, _pathsParsedList, err = getListInfo(&v, "list", _verticesParsedList, _edgesParsedList, _pathsParsedList)
if err != nil {
return _verticesParsedList, _edgesParsedList, _pathsParsedList, err
}
} else if vType == "map" {
var items = make(map[string]common.Any)
items, err = getMapInfo(&v, items)
data[k] = items
_verticesParsedList, _edgesParsedList, _pathsParsedList, err = getMapInfo(&v, _verticesParsedList, _edgesParsedList, _pathsParsedList)
if err != nil {
return _verticesParsedList, _edgesParsedList, _pathsParsedList, err
}
} else if vType == "set" {
var items = make([]common.Any, 0)
items, err = getListInfo(&v, items, "set")
data[k] = items
} else {
basicVal, err := getBasicValue(&v)
_verticesParsedList, _edgesParsedList, _pathsParsedList, err = getListInfo(&v, "set", _verticesParsedList, _edgesParsedList, _pathsParsedList)
if err != nil {
return data, err
return _verticesParsedList, _edgesParsedList, _pathsParsedList, err
}
data[k] = basicVal
} else {
// no need to parse basic value now
}
}
return data, nil
return _verticesParsedList, _edgesParsedList, _pathsParsedList, nil
}

// Connect return if the nebula connect succeed
Expand Down Expand Up @@ -314,6 +345,9 @@ func Execute(nsid string, gql string) (result ExecuteResult, err error) {
for i := 0; i < rowSize; i++ {
var rowValue = make(map[string]common.Any)
record, err := resp.GetRowValuesByIndex(i)
var _verticesParsedList = make([]common.Any, 0)
var _edgesParsedList = make([]common.Any, 0)
var _pathsParsedList = make([]common.Any, 0)
if err != nil {
return result, err
}
Expand All @@ -329,26 +363,35 @@ func Execute(nsid string, gql string) (result ExecuteResult, err error) {
rowValue[result.Headers[j]] = value
valueType := rowData.GetType()
if valueType == "vertex" {
rowValue, err = getVertexInfo(rowData, rowValue)
rowValue["type"] = "vertex"
var parseValue = make(map[string]common.Any)
parseValue, err = getVertexInfo(rowData, parseValue)
parseValue["type"] = "vertex"
_verticesParsedList = append(_verticesParsedList, parseValue)
} else if valueType == "edge" {
rowValue, err = getEdgeInfo(rowData, rowValue)
rowValue["type"] = "edge"
var parseValue = make(map[string]common.Any)
parseValue, err = getEdgeInfo(rowData, parseValue)
parseValue["type"] = "edge"
_edgesParsedList = append(_edgesParsedList, parseValue)
} else if valueType == "path" {
rowValue, err = getPathInfo(rowData, rowValue)
rowValue["type"] = "path"
var parseValue = make(map[string]common.Any)
parseValue, err = getPathInfo(rowData, parseValue)
parseValue["type"] = "path"
_pathsParsedList = append(_pathsParsedList, parseValue)
} else if valueType == "list" {
var info = make([]common.Any, 0)
info, err = getListInfo(rowData, info, "list")
rowValue[result.Headers[j]+"_info"] = info
_verticesParsedList, _edgesParsedList, _pathsParsedList, err = getListInfo(rowData, "list", _verticesParsedList, _edgesParsedList, _pathsParsedList)
} else if valueType == "set" {
var info = make([]common.Any, 0)
info, err = getListInfo(rowData, info, "set")
rowValue[result.Headers[j]+"_info"] = info
_verticesParsedList, _edgesParsedList, _pathsParsedList, err = getListInfo(rowData, "set", _verticesParsedList, _edgesParsedList, _pathsParsedList)
} else if valueType == "map" {
var info = make(map[string]common.Any)
info, err = getMapInfo(rowData, info)
rowValue[result.Headers[j]+"_info"] = info
_verticesParsedList, _edgesParsedList, _pathsParsedList, err = getMapInfo(rowData, _verticesParsedList, _edgesParsedList, _pathsParsedList)
}
if len(_verticesParsedList) > 0 {
rowValue["_verticesParsedList"] = _verticesParsedList
}
if len(_edgesParsedList) > 0 {
rowValue["_edgesParsedList"] = _edgesParsedList
}
if len(_pathsParsedList) > 0 {
rowValue["_pathsParsedList"] = _pathsParsedList
}
if err != nil {
return result, err
Expand Down