-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
208 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package codec | ||
|
||
import "errors" | ||
|
||
type Chain struct { | ||
Codecs []Codec | ||
} | ||
|
||
func NewChain(codecs []Codec) (*Chain, error) { | ||
if len(codecs) == 0 { | ||
return nil, errors.New("at least one codec is required") | ||
} | ||
return &Chain{Codecs: codecs}, nil | ||
} | ||
|
||
func (c *Chain) NewEncoder() Transformer { | ||
encoders := make([]Transformer, len(c.Codecs)) | ||
for i, codec := range c.Codecs { | ||
encoders[i] = codec.NewEncoder() | ||
} | ||
|
||
return func(data []byte) []byte { | ||
result := data | ||
// Apply encoders in forward order | ||
for _, encoder := range encoders { | ||
result = encoder(result) | ||
} | ||
return result | ||
} | ||
} | ||
|
||
func (c *Chain) NewDecoder() Transformer { | ||
decoders := make([]Transformer, len(c.Codecs)) | ||
for i, codec := range c.Codecs { | ||
decoders[i] = codec.NewDecoder() | ||
} | ||
|
||
return func(data []byte) []byte { | ||
result := data | ||
// Apply decoders in reverse order | ||
for i := len(decoders) - 1; i >= 0; i-- { | ||
result = decoders[i](result) | ||
} | ||
return result | ||
} | ||
} |
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,81 @@ | ||
package codec | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestChain(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
codecs []Codec | ||
input []byte | ||
}{ | ||
{ | ||
name: "single codec", | ||
codecs: []Codec{ | ||
NewInverter(), | ||
}, | ||
input: []byte("hello"), | ||
}, | ||
{ | ||
name: "multiple codecs", | ||
codecs: []Codec{ | ||
NewInverter(), | ||
must(NewXorer([]byte("key"))), | ||
must(NewInjector(42, 100)), | ||
}, | ||
input: []byte("test data"), | ||
}, | ||
{ | ||
name: "empty input", | ||
codecs: []Codec{ | ||
NewInverter(), | ||
must(NewXorer([]byte("key"))), | ||
}, | ||
input: []byte{}, | ||
}, | ||
{ | ||
name: "large input", | ||
codecs: []Codec{ | ||
NewInverter(), | ||
must(NewXorer([]byte("test-key"))), | ||
must(NewInjector(42, 50)), | ||
}, | ||
input: generateRandomBytes(1000, 42), | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
chain, err := NewChain(tt.codecs) | ||
require.NoError(t, err) | ||
|
||
encoder := chain.NewEncoder() | ||
decoder := chain.NewDecoder() | ||
|
||
// Test full array encode/decode | ||
encoded := encoder(tt.input) | ||
decoded := decoder(encoded) | ||
require.Equal(t, tt.input, decoded) | ||
|
||
// Test chunked encode/decode | ||
encodedChunks := transformByChunks(encoder, tt.input, 7) | ||
decodedChunks := transformByChunks(decoder, encodedChunks, 13) | ||
require.Equal(t, tt.input, decodedChunks) | ||
}) | ||
} | ||
} | ||
|
||
func TestChainValidation(t *testing.T) { | ||
_, err := NewChain(nil) | ||
require.Error(t, err) | ||
|
||
_, err = NewChain([]Codec{}) | ||
require.Error(t, err) | ||
|
||
chain, err := NewChain([]Codec{NewInverter()}) | ||
require.NoError(t, err) | ||
require.NotNil(t, chain) | ||
} |
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