Skip to content

Commit

Permalink
more wrappers
Browse files Browse the repository at this point in the history
  • Loading branch information
moukoublen committed Dec 5, 2023
1 parent 1d336d3 commit bff272a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 20 deletions.
31 changes: 19 additions & 12 deletions pick.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package pick

import (
"bytes"
"encoding/json"
"io"

"github.com/moukoublen/pick/cast"
)
Expand All @@ -15,22 +17,27 @@ type Traverser interface {
Get(m any, selector []SelectorKey) (any, error)
}

func Wrap(data any) *Picker {
caster := cast.NewCaster()
formatter := DefaultSelectorFormat{}
return NewPicker(data, NewDefaultTraverser(caster), caster, formatter)
func WrapJSON(js []byte) (*Picker, error) {
return WrapReaderJSON(bytes.NewReader(js))
}

func WrapJSON(js []byte) (*Picker, error) {
func WrapReaderJSON(r io.Reader) (*Picker, error) {
d := json.NewDecoder(r)
return WrapDecoder(d)
}

func WrapDecoder(decoder interface{ Decode(destination any) error }) (*Picker, error) {
m := map[string]any{}
err := json.Unmarshal(js, &m)
if err != nil {
if err := decoder.Decode(&m); err != nil {
return nil, err
}

return Wrap(m), nil
}

func Wrap(data any) *Picker {
caster := cast.NewCaster()
formatter := DefaultSelectorFormat{}
return NewPicker(m, NewDefaultTraverser(caster), caster, formatter), nil
return NewPicker(data, NewDefaultTraverser(caster), caster, DefaultSelectorFormat{})
}

type Picker struct {
Expand All @@ -40,12 +47,12 @@ type Picker struct {
selectorFormat SelectorFormat
}

func NewPicker(inner any, t Traverser, c Caster, selectorFormat SelectorFormat) *Picker {
func NewPicker(data any, t Traverser, c Caster, s SelectorFormat) *Picker {
return &Picker{
inner: inner,
inner: data,
traverser: t,
caster: c,
selectorFormat: selectorFormat,
selectorFormat: s,
}
}

Expand Down
20 changes: 12 additions & 8 deletions pick_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package pick

import (
"embed"
"encoding/json"
"fmt"
"io/fs"
"path/filepath"
"reflect"
"testing"
Expand Down Expand Up @@ -115,7 +115,7 @@ func TestString(t *testing.T) {
//go:embed internal/testingx/testdata
var testData embed.FS

func loadTestData(t *testing.T, filename string, decodeInto any) {
func loadTestData(t *testing.T, filename string) (fs.File, error) {

Check failure on line 118 in pick_test.go

View workflow job for this annotation

GitHub Actions / lint

loadTestData - result 1 (error) is always nil (unparam)
t.Helper()

path := filepath.Join("internal", "testingx", "testdata", filename)
Expand All @@ -124,17 +124,21 @@ func loadTestData(t *testing.T, filename string, decodeInto any) {
t.Fatalf("error during testdate file opening %s", err.Error())
}

if err := json.NewDecoder(f).Decode(decodeInto); err != nil {
t.Fatalf("error during testdate file decoding %s", err.Error())
}
return f, nil
}

func TestNasaDataFile(t *testing.T) {
t.Parallel()
inner := map[string]any{}
loadTestData(t, "nasa.json", &inner)

ob := Wrap(inner)
file, err := loadTestData(t, "nasa.json")
if err != nil {
t.Fatal(err)
}

ob, err := WrapReaderJSON(file)
if err != nil {
t.Fatal(err)
}

tests := []struct {
accessFn any
Expand Down

0 comments on commit bff272a

Please sign in to comment.