-
Notifications
You must be signed in to change notification settings - Fork 216
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
3 changed files
with
58 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package openAI | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"io" | ||
|
||
"github.com/eolinker/eosc" | ||
|
||
"github.com/andybalholm/brotli" | ||
) | ||
|
||
type IEncoder interface { | ||
ToUTF8([]byte) ([]byte, error) | ||
} | ||
|
||
type EncoderManger struct { | ||
encoders eosc.Untyped[string, IEncoder] | ||
} | ||
|
||
func NewEncoderManger() *EncoderManger { | ||
return &EncoderManger{encoders: eosc.BuildUntyped[string, IEncoder]()} | ||
} | ||
|
||
func (e *EncoderManger) Set(name string, encoder IEncoder) { | ||
e.encoders.Set(name, encoder) | ||
} | ||
|
||
func (e *EncoderManger) ToUTF8(name string, data []byte) ([]byte, error) { | ||
encoder, ok := e.encoders.Get(name) | ||
if !ok { | ||
return nil, fmt.Errorf("encoder %s not found", name) | ||
} | ||
return encoder.ToUTF8(data) | ||
} | ||
|
||
var encoderManger = NewEncoderManger() | ||
|
||
func init() { | ||
encoderManger.Set("br", &Br{}) | ||
} | ||
|
||
type Br struct { | ||
} | ||
|
||
func (b *Br) ToUTF8(data []byte) ([]byte, error) { | ||
reader := brotli.NewReader(bytes.NewReader(data)) | ||
return io.ReadAll(reader) | ||
} |
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