Skip to content

Commit 5577430

Browse files
committed
share capability return validation helpers
1 parent e7b5c34 commit 5577430

2 files changed

Lines changed: 15 additions & 15 deletions

File tree

vibes/capability_common.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,19 @@ func validateCapabilityHashValue(label string, val Value) error {
6565
return validateCapabilityTypedValue(label, val, capabilityTypeHash)
6666
}
6767

68+
func capabilityValidateAnyReturn(method string) func(result Value) error {
69+
return func(result Value) error {
70+
return validateCapabilityTypedValue(method+" return value", result, capabilityTypeAny)
71+
}
72+
}
73+
74+
func cloneCapabilityMethodResult(method string, result Value) (Value, error) {
75+
if err := validateCapabilityTypedValue(method+" return value", result, capabilityTypeAny); err != nil {
76+
return NewNil(), err
77+
}
78+
return deepCloneValue(result), nil
79+
}
80+
6881
func isNilCapabilityImplementation(impl any) bool {
6982
if impl == nil {
7083
return true

vibes/capability_events.go

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func (c *eventsCapability) CapabilityContracts() map[string]CapabilityMethodCont
4747
return map[string]CapabilityMethodContract{
4848
method: {
4949
ValidateArgs: c.validatePublishContractArgs,
50-
ValidateReturn: c.validateMethodReturn(method),
50+
ValidateReturn: capabilityValidateAnyReturn(method),
5151
},
5252
}
5353
}
@@ -73,7 +73,7 @@ func (c *eventsCapability) callPublish(exec *Execution, receiver Value, args []V
7373
if err != nil {
7474
return NewNil(), err
7575
}
76-
return c.cloneMethodResult(c.name+".publish", result)
76+
return cloneCapabilityMethodResult(c.name+".publish", result)
7777
}
7878

7979
func (c *eventsCapability) validatePublishContractArgs(args []Value, kwargs map[string]Value, block Value) error {
@@ -92,16 +92,3 @@ func (c *eventsCapability) validatePublishContractArgs(args []Value, kwargs map[
9292
}
9393
return validateCapabilityKwargsDataOnly(method, kwargs)
9494
}
95-
96-
func (c *eventsCapability) validateMethodReturn(method string) func(result Value) error {
97-
return func(result Value) error {
98-
return validateCapabilityTypedValue(method+" return value", result, capabilityTypeAny)
99-
}
100-
}
101-
102-
func (c *eventsCapability) cloneMethodResult(method string, result Value) (Value, error) {
103-
if err := validateCapabilityTypedValue(method+" return value", result, capabilityTypeAny); err != nil {
104-
return NewNil(), err
105-
}
106-
return deepCloneValue(result), nil
107-
}

0 commit comments

Comments
 (0)