Skip to content

Commit 9db36d2

Browse files
authored
feat: Encodable (#123)
- [x] Create a new `Encodable` interface with an `Encode` function matching the current `encode()` function call. - [x] Change `encode` to check if the given object is `Encodable` and using its `Encode` function if it is instead, bypassing all the encoder code. - [x] Create the `EncoderConfig` struct to be able to forward the current configuration simply, should any `Encodable` be there. - [x] Promote `bindings.WAFObject` to `libddwaf.WAFObject` - [x] Add more helper methods on the `WAFObject` type --------- Signed-off-by: Eliott Bouhana <[email protected]>
1 parent a015dbf commit 9db36d2

File tree

7 files changed

+295
-130
lines changed

7 files changed

+295
-130
lines changed

builder.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,11 @@ func (b *Builder) AddOrUpdateConfig(path string, fragment any) (Diagnostics, err
7272
var pinner runtime.Pinner
7373
defer pinner.Unpin()
7474

75-
encoder := newMaxEncoder(&pinner)
75+
encoder, err := newEncoder(newUnlimitedEncoderConfig(&pinner))
76+
if err != nil {
77+
return Diagnostics{}, fmt.Errorf("could not create encoder: %w", err)
78+
}
79+
7680
frag, err := encoder.Encode(fragment)
7781
if err != nil {
7882
return Diagnostics{}, fmt.Errorf("could not encode the config fragment into a WAF object; %w", err)

context.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package libddwaf
77

88
import (
9+
"fmt"
910
"maps"
1011
"runtime"
1112
"sync"
@@ -192,11 +193,15 @@ func merge[K comparable, V any](a, b map[K][]V) (merged map[K][]V) {
192193
// ephemeral addresses are allowed to be null one at a time. In this case, Encode will return nil,
193194
// which is what we need to send to ddwaf_run to signal that the address data is empty.
194195
func (context *Context) encodeOneAddressType(pinner pin.Pinner, addressData map[string]any, timer timer.Timer) (*bindings.WAFObject, error) {
195-
encoder := newLimitedEncoder(pinner, timer)
196196
if addressData == nil {
197197
return nil, nil
198198
}
199199

200+
encoder, err := newEncoder(newEncoderConfig(pinner, timer))
201+
if err != nil {
202+
return nil, fmt.Errorf("could not create encoder: %w", err)
203+
}
204+
200205
data, _ := encoder.Encode(addressData)
201206
if len(encoder.truncations) > 0 {
202207
context.mutex.Lock()

0 commit comments

Comments
 (0)