Skip to content

Commit

Permalink
feat: lazy initialization codec
Browse files Browse the repository at this point in the history
  • Loading branch information
xieyuschen committed Aug 18, 2023
1 parent 097e0d8 commit 6be49d9
Show file tree
Hide file tree
Showing 15 changed files with 371 additions and 383 deletions.
11 changes: 11 additions & 0 deletions internal/encoding/codec/codec.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package codec

type Codec interface {
// Decode decodes the contents of b into v.
// It's primarily used for decoding contents of a file into a map[string]interface{}.
Decode(b []byte, v map[string]interface{}) error

// Encode encodes the contents of v into a byte representation.
// It's primarily used for encoding a map[string]interface{} into a file format.
Encode(v map[string]interface{}) ([]byte, error)
}
61 changes: 0 additions & 61 deletions internal/encoding/decoder.go

This file was deleted.

81 changes: 0 additions & 81 deletions internal/encoding/decoder_test.go

This file was deleted.

11 changes: 8 additions & 3 deletions internal/encoding/dotenv/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,21 @@ import (
"sort"
"strings"

"github.com/spf13/viper/internal/encoding/codec"
"github.com/subosito/gotenv"
)

const keyDelimiter = "_"

// Codec implements the encoding.Encoder and encoding.Decoder interfaces for encoding data containing environment variables
// Codec implements the Codec interface for encoding data containing environment variables
// (commonly called as dotenv format).
type Codec struct{}

func (Codec) Encode(v map[string]interface{}) ([]byte, error) {
func New(args ...interface{}) codec.Codec {
return &Codec{}
}

func (*Codec) Encode(v map[string]interface{}) ([]byte, error) {
flattened := map[string]interface{}{}

flattened = flattenAndMergeMap(flattened, v, "", keyDelimiter)
Expand All @@ -40,7 +45,7 @@ func (Codec) Encode(v map[string]interface{}) ([]byte, error) {
return buf.Bytes(), nil
}

func (Codec) Decode(b []byte, v map[string]interface{}) error {
func (*Codec) Decode(b []byte, v map[string]interface{}) error {
var buf bytes.Buffer

_, err := buf.Write(b)
Expand Down
60 changes: 0 additions & 60 deletions internal/encoding/encoder.go

This file was deleted.

70 changes: 0 additions & 70 deletions internal/encoding/encoder_test.go

This file was deleted.

12 changes: 9 additions & 3 deletions internal/encoding/hcl/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,21 @@ import (
"bytes"
"encoding/json"

"github.com/spf13/viper/internal/encoding/codec"

"github.com/hashicorp/hcl"
"github.com/hashicorp/hcl/hcl/printer"
)

// Codec implements the encoding.Encoder and encoding.Decoder interfaces for HCL encoding.
// Codec implements the encoding.Codec interface for HCL encoding.
// TODO: add printer config to the codec?
type Codec struct{}

func (Codec) Encode(v map[string]interface{}) ([]byte, error) {
func New(args ...interface{}) codec.Codec {
return &Codec{}
}

func (*Codec) Encode(v map[string]interface{}) ([]byte, error) {
b, err := json.Marshal(v)
if err != nil {
return nil, err
Expand All @@ -35,6 +41,6 @@ func (Codec) Encode(v map[string]interface{}) ([]byte, error) {
return buf.Bytes(), nil
}

func (Codec) Decode(b []byte, v map[string]interface{}) error {
func (*Codec) Decode(b []byte, v map[string]interface{}) error {
return hcl.Unmarshal(b, &v)
}
Loading

0 comments on commit 6be49d9

Please sign in to comment.