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

Introduce LoadNebulaRow into the package #296

Closed
haoxins opened this issue Dec 11, 2023 · 4 comments
Closed

Introduce LoadNebulaRow into the package #296

haoxins opened this issue Dec 11, 2023 · 4 comments
Labels
type/enhancement Type: make the code neat or more efficient

Comments

@haoxins
Copy link
Collaborator

haoxins commented Dec 11, 2023

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.

@wey-gu
Copy link

wey-gu commented Dec 11, 2023

Wow great job @haoxins !

@veezhang @Nicole00 what do you think, please?

@haoxins
Copy link
Collaborator Author

haoxins commented Dec 19, 2023

I raised a draft PR for discussing the implementation details.

#298

@QingZ11 QingZ11 added the type/enhancement Type: make the code neat or more efficient label Dec 21, 2023
@veezhang
Copy link
Contributor

Gooooood idea!

@haoxins
Copy link
Collaborator Author

haoxins commented Jan 16, 2024

closed by #298

@haoxins haoxins closed this as completed Jan 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type/enhancement Type: make the code neat or more efficient
Projects
None yet
Development

No branches or pull requests

4 participants