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

feat(#10): collecting and sending failed links back to cloud provider via server #20

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
2 changes: 1 addition & 1 deletion examples/keyvalue-inmemory/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/wasmCloud/provider-sdk-go/examples/keyvalue-inmemory

go 1.22.3
go 1.20
tom-fitz marked this conversation as resolved.
Show resolved Hide resolved

require (
github.com/wasmCloud/provider-sdk-go v0.0.0-20240124183610-1a92f8d04935
Expand Down
6 changes: 4 additions & 2 deletions examples/keyvalue-inmemory/keyvalue.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ var (

type Provider struct {
sync.Map
sourceLinks map[string]provider.InterfaceLinkDefinition
targetLinks map[string]provider.InterfaceLinkDefinition
sourceLinks map[string]provider.InterfaceLinkDefinition
targetLinks map[string]provider.InterfaceLinkDefinition
failedSourceLinks map[string]provider.InterfaceLinkDefinition
failedTargetLinks map[string]provider.InterfaceLinkDefinition
}

func Ok[T any](v T) *wrpc.Result[T, store.Error] {
Expand Down
44 changes: 44 additions & 0 deletions examples/keyvalue-inmemory/link.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package main

import (
"github.com/wasmCloud/provider-sdk-go"
"log"
)

func (p *Provider) establishSourceLink(link provider.InterfaceLinkDefinition) error {
if _, exists := p.sourceLinks[link.Target]; exists {
log.Println("Source link already exists, ignoring duplicate", link)
return nil
}
brooksmtownsend marked this conversation as resolved.
Show resolved Hide resolved

if err := p.validateSourceLink(link); err != nil {
return err
}

p.sourceLinks[link.Target] = link
return nil
}

func (p *Provider) establishTargetLink(link provider.InterfaceLinkDefinition) error {
if _, exists := p.targetLinks[link.SourceID]; exists {
log.Println("Target link already exists, ignoring duplicate", link)
return nil
}
tom-fitz marked this conversation as resolved.
Show resolved Hide resolved

if err := p.validateTargetLink(link); err != nil {
return err
}

p.targetLinks[link.SourceID] = link
return nil
}

func (p *Provider) validateSourceLink(link provider.InterfaceLinkDefinition) error {
// TODO: Add validation checks
return nil
}

func (p *Provider) validateTargetLink(link provider.InterfaceLinkDefinition) error {
// TODO: Add validation checks
return nil
}
18 changes: 16 additions & 2 deletions examples/keyvalue-inmemory/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ func main() {

func run() error {
p := &Provider{
sourceLinks: make(map[string]provider.InterfaceLinkDefinition),
targetLinks: make(map[string]provider.InterfaceLinkDefinition),
sourceLinks: make(map[string]provider.InterfaceLinkDefinition),
targetLinks: make(map[string]provider.InterfaceLinkDefinition),
failedSourceLinks: make(map[string]provider.InterfaceLinkDefinition),
failedTargetLinks: make(map[string]provider.InterfaceLinkDefinition),
}

wasmcloudprovider, err := provider.New(
Expand Down Expand Up @@ -69,12 +71,24 @@ func run() error {

func (p *Provider) handleNewSourceLink(link provider.InterfaceLinkDefinition) error {
log.Println("Handling new source link", link)
err := p.establishSourceLink(link)
if err != nil {
log.Println("Failed to establish source link", link, err)
p.failedSourceLinks[link.Target] = link
brooksmtownsend marked this conversation as resolved.
Show resolved Hide resolved
return err
}
p.sourceLinks[link.Target] = link
return nil
}

func (p *Provider) handleNewTargetLink(link provider.InterfaceLinkDefinition) error {
log.Println("Handling new target link", link)
err := p.establishTargetLink(link)
if err != nil {
log.Println("Failed to establish target link", link, err)
p.failedTargetLinks[link.SourceID] = link
brooksmtownsend marked this conversation as resolved.
Show resolved Hide resolved
return err
}
p.targetLinks[link.SourceID] = link
return nil
}
Expand Down