diff --git a/examples/keyvalue-inmemory/keyvalue.go b/examples/keyvalue-inmemory/keyvalue.go index ac38890..9298070 100644 --- a/examples/keyvalue-inmemory/keyvalue.go +++ b/examples/keyvalue-inmemory/keyvalue.go @@ -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] { diff --git a/examples/keyvalue-inmemory/link.go b/examples/keyvalue-inmemory/link.go new file mode 100644 index 0000000..70271af --- /dev/null +++ b/examples/keyvalue-inmemory/link.go @@ -0,0 +1,80 @@ +package main + +import ( + "github.com/wasmCloud/provider-sdk-go" + "log" +) + +func (p *Provider) establishSourceLink(link provider.InterfaceLinkDefinition) error { + 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 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 +} + +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 +} + +func (p *Provider) handleDelSourceLink(link provider.InterfaceLinkDefinition) error { + log.Println("Handling del source link", link) + delete(p.sourceLinks, link.Target) + return nil +} + +func (p *Provider) handleDelTargetLink(link provider.InterfaceLinkDefinition) error { + log.Println("Handling del target link", link) + delete(p.targetLinks, link.SourceID) + return nil +} + +func (p *Provider) handleHealthCheck() string { + log.Println("Handling health check") + return "provider healthy" +} + +func (p *Provider) handleShutdown() error { + log.Println("Handling shutdown") + return nil +} diff --git a/examples/keyvalue-inmemory/main.go b/examples/keyvalue-inmemory/main.go index a216b31..d1bc42d 100644 --- a/examples/keyvalue-inmemory/main.go +++ b/examples/keyvalue-inmemory/main.go @@ -1,5 +1,3 @@ -//go:generate wit-bindgen-wrpc go --out-dir bindings --package github.com/wasmCloud/provider-sdk-go/examples/keyvalue-inmemory/bindings wit - package main import ( @@ -20,8 +18,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( @@ -66,37 +66,3 @@ func run() error { return nil } - -func (p *Provider) handleNewSourceLink(link provider.InterfaceLinkDefinition) error { - log.Println("Handling new source link", link) - p.sourceLinks[link.Target] = link - return nil -} - -func (p *Provider) handleNewTargetLink(link provider.InterfaceLinkDefinition) error { - log.Println("Handling new target link", link) - p.targetLinks[link.SourceID] = link - return nil -} - -func (p *Provider) handleDelSourceLink(link provider.InterfaceLinkDefinition) error { - log.Println("Handling del source link", link) - delete(p.sourceLinks, link.Target) - return nil -} - -func (p *Provider) handleDelTargetLink(link provider.InterfaceLinkDefinition) error { - log.Println("Handling del target link", link) - delete(p.targetLinks, link.SourceID) - return nil -} - -func (p *Provider) handleHealthCheck() string { - log.Println("Handling health check") - return "provider healthy" -} - -func (p *Provider) handleShutdown() error { - log.Println("Handling shutdown") - return nil -} diff --git a/provider.go b/provider.go index 69a8579..82520e8 100644 --- a/provider.go +++ b/provider.go @@ -97,8 +97,8 @@ func New(options ...ProviderHandler) (*WasmcloudProvider, error) { } // partition links based on if the provider is the source or target - sourceLinks := []InterfaceLinkDefinition{} - targetLinks := []InterfaceLinkDefinition{} + var sourceLinks []InterfaceLinkDefinition + var targetLinks []InterfaceLinkDefinition // Loop over the numbers for _, link := range hostData.LinkDefinitions { @@ -344,20 +344,11 @@ func (wp *WasmcloudProvider) putLink(l InterfaceLinkDefinition) error { wp.lock.Lock() defer wp.lock.Unlock() - if l.SourceID == wp.Id { - err := wp.putSourceLinkFunc(l) - if err != nil { - return err - } - wp.sourceLinks[l.Target] = l + if l.SourceID == wp.Id { + return wp.putSourceLinkFunc(l) } else if l.Target == wp.Id { - err := wp.putTargetLinkFunc(l) - if err != nil { - return err - } - - wp.targetLinks[l.SourceID] = l + return wp.putTargetLinkFunc(l) } else { wp.Logger.Info("received link that isn't for this provider, ignoring", "link", l) } @@ -369,23 +360,12 @@ func (wp *WasmcloudProvider) deleteLink(l InterfaceLinkDefinition) error { wp.lock.Lock() defer wp.lock.Unlock() if l.SourceID == wp.Id { - err := wp.delSourceLinkFunc(l) - if err != nil { - return err - } - - delete(wp.sourceLinks, l.Target) + return wp.delSourceLinkFunc(l) } else if l.Target == wp.Id { - err := wp.delTargetLinkFunc(l) - if err != nil { - return err - } - - delete(wp.targetLinks, l.SourceID) + return wp.delTargetLinkFunc(l) } else { wp.Logger.Info("received link delete that isn't for this provider, ignoring", "link", l) } - return nil }