Skip to content

Commit

Permalink
[WIP] feat: change api structure (#19)
Browse files Browse the repository at this point in the history
* feat: change api structure

* mod: code review
  • Loading branch information
hetao92 authored Apr 23, 2021
1 parent 58d4f92 commit f60f6cf
Showing 1 changed file with 98 additions and 53 deletions.
151 changes: 98 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 All @@ -16,6 +17,8 @@ type ExecuteResult struct {
TimeCost int32 `json:"timeCost"`
}

type list []common.Any

func getID(idWarp nebula.ValueWrapper) common.Any {
idType := idWarp.GetType()
var vid common.Any
Expand Down Expand Up @@ -160,7 +163,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 *list, _edgesParsedList *list, _pathsParsedList *list) error {
var valueList []nebula.ValueWrapper
var err error
if listType == "list" {
Expand All @@ -169,76 +172,106 @@ func getListInfo(valWarp *nebula.ValueWrapper, data []common.Any, listType strin
valueList, err = valWarp.AsDedupList()
}
if err != nil {
return nil, err
return 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 err
}
} else if vType == "edge" {
props, err = getEdgeInfo(&v, props)
if err == nil {
*_edgesParsedList = append(*_edgesParsedList, props)
} else {
return err
}
} else if vType == "path" {
props, err = getPathInfo(&v, props)
if err == nil {
*_pathsParsedList = append(*_pathsParsedList, props)
} else {
return err
}
} else if vType == "list" {
var items = make([]common.Any, 0)
items, err = getListInfo(&v, items, "list")
props["items"] = items
err = getListInfo(&v, "list", _verticesParsedList, _edgesParsedList, _pathsParsedList)
if err != nil {
return err
}
} else if vType == "map" {
var items = make(map[string]common.Any)
items, err = getMapInfo(&v, items)
props["items"] = items
err = getMapInfo(&v, _verticesParsedList, _edgesParsedList, _pathsParsedList)
if err != nil {
return 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)
err = getListInfo(&v, "set", _verticesParsedList, _edgesParsedList, _pathsParsedList)
if err != nil {
return data, err
return err
}
props["value"] = basicVal
} else {
// no need to parse basic value now
}
data = append(data, props)
}
return data, nil
return nil
}

func getMapInfo(valWarp *nebula.ValueWrapper, data map[string]common.Any) (map[string]common.Any, error) {
func getMapInfo(valWarp *nebula.ValueWrapper, _verticesParsedList *list, _edgesParsedList *list, _pathsParsedList *list) error {
valueMap, err := valWarp.AsMap()
if err != nil {
return nil, err
return 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 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 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 err
}
} else if vType == "list" {
var items = make([]common.Any, 0)
items, err = getListInfo(&v, items, "list")
data[k] = items
err = getListInfo(&v, "list", _verticesParsedList, _edgesParsedList, _pathsParsedList)
if err != nil {
return err
}
} else if vType == "map" {
var items = make(map[string]common.Any)
items, err = getMapInfo(&v, items)
data[k] = items
err = getMapInfo(&v, _verticesParsedList, _edgesParsedList, _pathsParsedList)
if err != nil {
return 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)
err = getListInfo(&v, "set", _verticesParsedList, _edgesParsedList, _pathsParsedList)
if err != nil {
return data, err
return err
}
data[k] = basicVal
} else {
// no need to parse basic value now
}
}
return data, nil
return nil
}

// Connect return if the nebula connect succeed
Expand Down Expand Up @@ -314,6 +347,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(list, 0)
var _edgesParsedList = make(list, 0)
var _pathsParsedList = make(list, 0)
if err != nil {
return result, err
}
Expand All @@ -329,26 +365,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
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
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
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

0 comments on commit f60f6cf

Please sign in to comment.