-
-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move all cap table methods from Message to CapTable.
- Loading branch information
Showing
24 changed files
with
195 additions
and
153 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package capnp | ||
|
||
type CapTable struct { | ||
cs []Client | ||
} | ||
|
||
func (ct *CapTable) Reset(cs ...Client) { | ||
for _, c := range ct.cs { | ||
c.Release() | ||
} | ||
|
||
ct.cs = append(ct.cs[:0], cs...) | ||
} | ||
|
||
func (ct CapTable) Len() int { | ||
return len(ct.cs) | ||
} | ||
|
||
func (ct CapTable) At(i int) Client { | ||
return ct.cs[i] | ||
} | ||
|
||
func (ct CapTable) Contains(ifc Interface) bool { | ||
return ifc.IsValid() && ifc.Capability() < CapabilityID(ct.Len()) | ||
} | ||
|
||
func (ct CapTable) Get(ifc Interface) (c Client) { | ||
if ct.Contains(ifc) { | ||
c = ct.cs[ifc.Capability()] | ||
} | ||
|
||
return | ||
} | ||
|
||
func (ct CapTable) Set(id CapabilityID, c Client) { | ||
ct.cs[id] = c | ||
} | ||
|
||
// Add appends a capability to the message's capability table and | ||
// returns its ID. It "steals" c's reference: the Message will release | ||
// the client when calling Reset. | ||
func (ct *CapTable) Add(c Client) CapabilityID { | ||
ct.cs = append(ct.cs, c) | ||
return CapabilityID(ct.Len() - 1) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package capnp_test | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
"capnproto.org/go/capnp/v3" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestCapTable(t *testing.T) { | ||
t.Parallel() | ||
|
||
var ct capnp.CapTable | ||
|
||
assert.Zero(t, ct.Len(), | ||
"zero-value CapTable should be empty") | ||
assert.Zero(t, ct.Add(capnp.Client{}), | ||
"first entry should have CapabilityID(0)") | ||
assert.Equal(t, 1, ct.Len(), | ||
"should increase length after adding capability") | ||
|
||
ct.Reset() | ||
assert.Zero(t, ct.Len(), | ||
"zero-value CapTable should be empty after Reset()") | ||
ct.Reset(capnp.Client{}, capnp.Client{}) | ||
assert.Equal(t, 2, ct.Len(), | ||
"zero-value CapTable should be empty after Reset(c, c)") | ||
|
||
errTest := errors.New("test") | ||
ct.Set(capnp.CapabilityID(0), capnp.ErrorClient(errTest)) | ||
err := ct.At(0).State().Brand.Value.(error) | ||
assert.ErrorIs(t, errTest, err, "should update client at index 0") | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.