Skip to content

Commit

Permalink
Rename manifest fields, add example setting (#4)
Browse files Browse the repository at this point in the history
* rename manifest fields, add example setting

* fix manifest version

* parseSettings in a struct

---------

Co-authored-by: Clement Bouvet <clement@cbouvet.fr>
  • Loading branch information
alexcasalboni and CLEMENTINATOR authored Feb 14, 2025
1 parent a1da0aa commit 2f5eab5
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 13 deletions.
39 changes: 29 additions & 10 deletions dc_component.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,35 @@ import (
"go.bytecodealliance.org/cm"
)

type Settings struct {
Example string
}

func parseSettings(settings dc.Dict) *Settings {
slice := settings.Slice()

example := ""

for _, v := range slice {
if v[0] == "example" {
example = v[1]
}
}

return &Settings{
Example: example,
}
}

// Implement the datacollection.Exports.Page, datacollection.Exports.Track, and datacollection.Exports.User functions.
// These functions are called by the Edgee runtime to get the HTTP request to make to the provider's API for each event type.
func PageHandler(e dc.Event, settings dc.Dict) dc.EdgeeRequest {
// Access settings by using the Slice method
// For example, if you component is setup as following:
// [[components.data_collection]]
// name = "my_component"
// component = "outpout.wasm"
// settings.test_project_id = "123456789"
// settings.test_write_key = "abcdefg"
// Then
// settings.Slice() will return a slice of tuples with the following values:
// [["test_project_id", "123456789"], ["test_write_key", "abcdefg"]]
parsedSettings := parseSettings(settings)

headers := [][2]string{
{"Content-Type", "application/json"},
{"Authorization", "Bearer token123"},
{"Example", parsedSettings.Example},
}
list := cm.NewList(&headers[0], len(headers))
dict := dc.Dict(list)
Expand All @@ -36,9 +49,12 @@ func PageHandler(e dc.Event, settings dc.Dict) dc.EdgeeRequest {
}

func TrackHandler(e dc.Event, settings dc.Dict) dc.EdgeeRequest {
parsedSettings := parseSettings(settings)

headers := [][2]string{
{"Content-Type", "application/json"},
{"Authorization", "Bearer token123"},
{"Example", parsedSettings.Example},
}
list := cm.NewList(&headers[0], len(headers))
dict := dc.Dict(list)
Expand All @@ -54,9 +70,12 @@ func TrackHandler(e dc.Event, settings dc.Dict) dc.EdgeeRequest {
}

func UserHandler(e dc.Event, settings dc.Dict) dc.EdgeeRequest {
parsedSettings := parseSettings(settings)

headers := [][2]string{
{"Content-Type", "application/json"},
{"Authorization", "Bearer token123"},
{"Example", parsedSettings.Example},
}
list := cm.NewList(&headers[0], len(headers))
dict := dc.Dict(list)
Expand Down
10 changes: 7 additions & 3 deletions edgee-component.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
manifest_version = 1
manifest-version = 1

[package]
[component]
name = "example-go-component"
version = "1.0.0"
category = "data-collection"
Expand All @@ -10,6 +10,10 @@ documentation = "https://github.com/edgee-cloud/example-go-component"
repository = "https://github.com/edgee-cloud/example-go-component"
wit-world-version = "0.4.0"

[package.build]
[component.build]
command = "go run go.bytecodealliance.org/cmd/wit-bindgen-go generate -o internal/ ./wit && tinygo build -target=wasip2 -o dc_component.wasm --wit-package wit/ --wit-world data-collection ./"
output_path = "./dc_component.wasm"

[component.settings.example]
title = "Example Config Field"
type = "string"

0 comments on commit 2f5eab5

Please sign in to comment.