Skip to content

Commit

Permalink
feat(#10): collecting and sending failed links back to cloud provider…
Browse files Browse the repository at this point in the history
… via server)
  • Loading branch information
tom-fitz committed Jun 5, 2024
1 parent 5423f4b commit 3c56a0e
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 5 deletions.
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

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
}

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
}

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
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
return err
}
p.targetLinks[link.SourceID] = link
return nil
}
Expand Down

0 comments on commit 3c56a0e

Please sign in to comment.