vesoft-inc/nebula-go

Introduce LoadNebulaRow into the package

haoxins opened this issue · 4 comments

Hi, in my own project, I wrote a method to work with Nebula row. It looks like this:

func LoadNebulaRow(colNames []string, row *nebulaType.Row, obj interface{}) interface{} {
	rowValues := row.GetValues()

	val := reflect.ValueOf(&obj).Elem()

	tmp := reflect.New(val.Elem().Type()).Elem()
	numField := reflect.ValueOf(obj).NumField()
	for i := 0; i < numField; i++ {
		fType := reflect.ValueOf(obj).Type().Field(i)
		tag := fType.Tag.Get("nebula")

		if tag == "" {
			continue
		}

		rowVal := rowValues[slices.Index(colNames, tag)]

		switch fType.Type.Kind() {
		case reflect.Int64:
			tmp.Field(i).SetInt(rowVal.GetIVal())
		case reflect.String:
			tmp.Field(i).SetString(string(rowVal.GetSVal()))
                // ............
		default:
			panic("not support type")
		}
	}

	val.Set(tmp)

	return obj
}

The usage could be:

type AccountVertex struct {
	Src               string `nebula:"src"`
	Dst               string `nebula:"dst"`
	LinkType          string `nebula:"link_type"`
	Status            string `nebula:"account_status"`
	Tel               string `nebula:"tel"`
	Email            string `nebula:"email"`
}

// ...
rs, err = NebulaClient.ExecQuery(q)
if err != nil {
        // ...
}

cols = rs.GetColNames()
rows = rs.GetRows()

var data []AccountVertex
for _, row := range rows {
	result := LoadNebulaRow(colNames, row, AccountVertex{})
	data = append(data, result.(AccountVertex))
}

This is a draft from my use case, I am not sure it is suitable to load into this client package.

Wow great job @haoxins !

@veezhang @Nicole00 what do you think, please?

I raised a draft PR for discussing the implementation details.

#298

Gooooood idea!

closed by #298