-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: integrate Viper's internal encoding package
Signed-off-by: Zsolt Rappi <[email protected]>
- Loading branch information
Showing
12 changed files
with
393 additions
and
34 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,66 @@ | ||
package encoding | ||
|
||
import ( | ||
"fmt" | ||
"sync" | ||
) | ||
|
||
// Encoder encodes the contents of v into a byte representation. | ||
// It's primarily used for encoding a map[string]interface{} into a file format. | ||
type Encoder interface { | ||
Encode(v map[string]interface{}) ([]byte, error) | ||
} | ||
|
||
const ( | ||
// ErrEncoderNotFound is returned when there is no encoder registered for a format. | ||
ErrEncoderNotFound = encodingError("encoder not found for this format") | ||
|
||
// ErrEncoderFormatAlreadyRegistered is returned when an encoder is already registered for a format. | ||
ErrEncoderFormatAlreadyRegistered = encodingError("encoder already registered for this format") | ||
) | ||
|
||
// EncoderRegistry can choose an appropriate Encoder based on the provided format. | ||
type EncoderRegistry struct { | ||
encoders map[string]Encoder | ||
|
||
mu sync.RWMutex | ||
} | ||
|
||
// NewEncoderRegistry returns a new, initialized EncoderRegistry. | ||
func NewEncoderRegistry() *EncoderRegistry { | ||
return &EncoderRegistry{ | ||
encoders: make(map[string]Encoder), | ||
} | ||
} | ||
|
||
// RegisterEncoder registers an Encoder for a format. | ||
// Registering a Encoder for an already existing format is not supported. | ||
func (e *EncoderRegistry) RegisterEncoder(format string, enc Encoder) error { | ||
e.mu.Lock() | ||
defer e.mu.Unlock() | ||
|
||
if _, ok := e.encoders[format]; ok { | ||
return ErrEncoderFormatAlreadyRegistered | ||
} | ||
|
||
e.encoders[format] = enc | ||
|
||
return nil | ||
} | ||
|
||
func (e *EncoderRegistry) Encode(format string, v map[string]interface{}) ([]byte, error) { | ||
e.mu.RLock() | ||
encoder, ok := e.encoders[format] | ||
e.mu.RUnlock() | ||
|
||
if !ok { | ||
return nil, ErrEncoderNotFound | ||
} | ||
|
||
b, err := encoder.Encode(v) | ||
if err != nil { | ||
return nil, fmt.Errorf("encoding map to %s format: %w", format, err) | ||
} | ||
|
||
return b, nil | ||
} |
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,7 @@ | ||
package encoding | ||
|
||
type encodingError string | ||
|
||
func (e encodingError) Error() string { | ||
return string(e) | ||
} |
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,24 @@ | ||
package json | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
) | ||
|
||
// Codec implements the encoding.Encoder interface for JSON encoding. | ||
type Codec struct { | ||
// prefix for JSON marshal. | ||
Prefix string | ||
|
||
// indentation for JSON marshal. | ||
Indent string | ||
} | ||
|
||
func (c *Codec) Encode(v map[string]interface{}) ([]byte, error) { | ||
b, err := json.MarshalIndent(v, c.Prefix, c.Indent) | ||
if err != nil { | ||
return nil, fmt.Errorf("json marshalling value: %w", err) | ||
} | ||
|
||
return b, nil | ||
} |
Oops, something went wrong.