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

bloblang.Executor.Query with a custom struct (not a map[string]interface{}) #1317

Closed
oliverpool opened this issue Jul 7, 2022 · 2 comments
Closed
Labels

Comments

@oliverpool
Copy link

Hi,
I am exploring if bloblang could be used inside gitea for webhook payload transformation (go-gitea/gitea#19307 (comment))

I have a working prototype, however it is quite hacky since it seems that bloblang.Executor.Query only works properly with an argument of type map[string]interface{} and not with arbitrary struct.

Code example:

func TestBloblang(t *testing.T) {
	env := bloblang.NewEnvironment().WithoutFunctions("env", "file")

	mapping := `
root.type = this.type()
root.this = this
root.sha = this.sha
root.Sha = this.Sha
`
	exe, err := env.Parse(mapping)
	if err != nil {
		panic(err)
	}
	res, err := exe.Query(struct {
		Sha     string `json:"sha"`
		Ref     string `json:"ref"`
		RefType string `json:"ref_type"`
	}{
		Sha:     "sha1",
		Ref:     "some/ref",
		RefType: "tags",
	})
	if err != nil {
		panic(err)
	}
	b, err := json.Marshal(res)
	if err != nil {
		panic(err)
	}
	t.Log(string(b))
	t.Fail() // so that t.Log is shown
}

Output:

--- FAIL: TestBloblang (0.00s)
    bloblang_test.go:48: {"Sha":null,"sha":null,"this":{"sha":"sha1","ref":"some/ref","ref_type":"tags"},"type":"unknown"}

Currently to get an object inside bloblang, I json-marshal my struct and unmarshal it in a map[string]interface{} object...
Is there any cleaner way to get a proper object inside bloblang without marshalling+unmarshalling?

@Jeffail
Copy link
Collaborator

Jeffail commented Jul 7, 2022

Hey @oliverpool, the bloblang engine doesn't support reflection of struct types when expanding fields so any data that you want mappings to be able to traverse needs to be in maps and empty interface slices.

Adding reflection is something I'd possibly be open to but for now it might be cleaner to use https://github.com/mitchellh/mapstructure as that'd save you the performance hit of byte serialization back and forth.

@oliverpool
Copy link
Author

Thank your for the quick reply!
I don't think that https://github.com/mitchellh/mapstructure works in my case, since I already have a struct, which I need to transform to a map[string]interface to use bloblang (and not the other way around).

I can use mapstructure on the result if I want to reconstruct a nice structure.

I proposed a fix in the gabs package: Jeffail/gabs#122 let me know what you think

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants