Resource export is unclear
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 30/100
Research direction
Start by comparing the old and latest wit-bindgen-go resource export APIs using the WIT counter example and the generated resourceexampleapi.Exports.Counter handlers. Determine whether cm.Rep and the manual counters map are intended, then document or propose the expected resource-lifecycle behavior and validate it with an applicable binding-generation test if one exists.
Written by the indexing model from the issue text.
Description
Hi!
I'm in the middle of migrating from the old binding generator in wit-bindgen to the latest version of wit-bindgen-go and got confused by what exactly new way of exporting resources is.
Let's take an example where we define and export a counter resource:
package resource:example;
interface api {
resource counter {
constructor(name: string);
add: func(value: u64);
get: func() -> u64;
}
}
world example {
export api;
}
In the old binding generator to export this, it was possible to write a Go struct with methods on it and a constructor on the exported type:
// struct representing the component
type ResourceExampleImpl struct {
}
// struct representing an instance of the resource
type Counter struct {
name string
current uint64
}
func (e *ResourceExampleImpl) ConstructorCounter(name string) resource_example.ExportsResourceExampleApiCounter {
return &Counter{
name: name,
current: 0,
}
}
func (e *Counter) MethodCounterAdd(value uint64) {
e.current += value
}
func (e *Counter) MethodCounterGet() uint64 {
return e.current
}
func init() {
resource_example.SetExportsResourceExampleApi(&ResourceExampleImpl{})
}
With the latest wit-bindgen-go the only way I found to do the same is to manually maintain a map of the generated resources indexed by cm.Rep:
type Counter struct {
name string
current uint64
}
func NewCounter(name string) *Counter {
return &Counter{
name: name,
current: 0,
}
}
func (e *Counter) Add(value uint64) {
e.current += value
}
func (e *Counter) Get() uint64 {
return e.current
}
var counters = make(map[cm.Rep]*Counter)
var lastId = cm.Rep(0)
func init() {
resourceexampleapi.Exports.Counter.Constructor = func(name string) (result resourceexampleapi.Counter) {
counter := NewCounter(name)
id := lastId + 1
lastId = id
counters[id] = counter
return resourceexampleapi.CounterResourceNew(id)
}
resourceexampleapi.Exports.Counter.Destructor = func(id cm.Rep) {
delete(counters, id)
}
resourceexampleapi.Exports.Counter.Add = func(id cm.Rep, value uint64) {
counters[id].Add(value)
}
resourceexampleapi.Exports.Counter.Get = func(id cm.Rep) uint64 {
return counters[id].Get()
}
}
Is this how it is supposed to work or am I missing something?
The new binding generator version is much nicer than the previous one in many ways, but having to manually keep track of resources seems weird to me.
- Dominant language
- Go
- Stars
- 148
- Forks
- 20
- PR merge metrics
- No merged PRs in 30d
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from bytecodealliance/go-modules
-
Difficulty 2/5 1-3 hours Newbie friendliness 48/100
bytecodealliance/go-modules#428 · 1 reaction ·
-
Difficulty 3/5 1-2 days Newbie friendliness 45/100
bytecodealliance/go-modules#411 ·
-
Difficulty 4/5 3-5 days Newbie friendliness 45/100
bytecodealliance/go-modules#410 ·
-
Difficulty 4/5 3-5 days Newbie friendliness 48/100
bytecodealliance/go-modules#394 ·
-
Difficulty 3/5 1-2 days Newbie friendliness 38/100
bytecodealliance/go-modules#377 · 1 comment ·
All issues in bytecodealliance/go-modules
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
Difficulty 1/5 Under an hour Newbie friendliness 84/100
-
enhancement needs triage
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
-
kind/cleanup
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
kubernetes-sigs/kueue#15947 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
sympozium-ai/sympozium#627 ·