Skip to content

Commit

Permalink
parseSettings in a struct
Browse files Browse the repository at this point in the history
  • Loading branch information
CLEMENTINATOR committed Feb 14, 2025
1 parent 32e7b7c commit 818b861
Showing 1 changed file with 29 additions and 10 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

0 comments on commit 818b861

Please sign in to comment.