Skip to content

Commit b139bbb

Browse files
committed
feat: add GetSuffleLines, add read file to Yaml.
1 parent c9980cf commit b139bbb

5 files changed

Lines changed: 42 additions & 8 deletions

File tree

content_operation.go

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ import (
66
"encoding/json"
77
"fmt"
88
"io"
9+
"math/rand"
910
"os"
1011
"strings"
1112

13+
"github.com/goccy/go-yaml"
1214
"github.com/spf13/afero"
1315
)
1416

@@ -128,6 +130,21 @@ func (p *FsPath) MustGetLines() []string {
128130
return lines
129131
}
130132

133+
// GetShuffleLines reads the file return its contents as a slice of shuffled strings
134+
func (p *FsPath) GetShuffleLines() ([]string, error) {
135+
lines, err := p.GetLines()
136+
if err != nil {
137+
return nil, err
138+
}
139+
140+
for i := range lines {
141+
j := rand.Intn(i + 1)
142+
lines[i], lines[j] = lines[j], lines[i]
143+
}
144+
145+
return lines, nil
146+
}
147+
131148
// GetJSON reads the file and unmarshals its content into the provided interface.
132149
//
133150
// This method reads the entire file content using GetBytes, then uses json.Unmarshal
@@ -155,13 +172,13 @@ func (p *FsPath) MustGetLines() []string {
155172
//
156173
// Note: This method reads the entire file into memory. For very large files,
157174
// consider using a streaming JSON parser instead.
158-
func (p *FsPath) GetJSON(raw interface{}) error {
175+
func (p *FsPath) GetJSON(v any) error {
159176
data, err := p.GetBytes()
160177
if err != nil {
161178
return err
162179
}
163180

164-
return json.Unmarshal(data, raw)
181+
return json.Unmarshal(data, v)
165182
}
166183

167184
// MustGetJSON reads the file, unmarshals its content into the provided interface, and panics on error.
@@ -184,10 +201,24 @@ func (p *FsPath) GetJSON(raw interface{}) error {
184201
//
185202
// Note: Use this method only when you're sure the file exists and contains valid JSON,
186203
// or if you want to halt execution on error.
187-
func (p *FsPath) MustGetJSON(v interface{}) {
204+
func (p *FsPath) MustGetJSON(v any) {
188205
p.e(p.GetJSON(v))
189206
}
190207

208+
// GetYaml similar as GetJSON, but unmarshal with Yaml
209+
func (p *FsPath) GetYaml(v any) error {
210+
data, err := p.GetBytes()
211+
if err != nil {
212+
return err
213+
}
214+
215+
return yaml.Unmarshal(data, &v)
216+
}
217+
218+
func (p *FsPath) MustGetYaml(v any) {
219+
p.e(p.GetYaml(v))
220+
}
221+
191222
// Reader returns an io.Reader for the file
192223
func (p *FsPath) Reader() (io.Reader, error) {
193224
return p.fs.Open(p.absPath)

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
module github.com/coghost/pathlib
22

3-
go 1.22.5
3+
go 1.24
44

55
require (
6+
github.com/goccy/go-yaml v1.17.1
67
github.com/spf13/afero v1.11.0
78
github.com/stretchr/testify v1.9.0
89
)

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
22
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
33
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
4+
github.com/goccy/go-yaml v1.17.1 h1:LI34wktB2xEE3ONG/2Ar54+/HJVBriAGJ55PHls4YuY=
5+
github.com/goccy/go-yaml v1.17.1/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA=
46
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
57
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
68
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=

path.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ func (p *FsPath) Suffixes() []string {
194194
// Example:
195195
//
196196
// p.e(someFunction()) // panics if someFunction returns an error
197-
func (p *FsPath) e(args ...interface{}) {
197+
func (p *FsPath) e(args ...any) {
198198
err, ok := args[len(args)-1].(error)
199199
if ok {
200200
panic(err)

util.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -245,14 +245,14 @@ func IsJSON(data []byte) bool {
245245
//
246246
// Note: This function will only include fields that would be marshaled to JSON.
247247
// Unexported fields and fields with `json:"-"` tags will be omitted.
248-
func StructToJSONMap(src interface{}) (map[string]interface{}, error) {
248+
func StructToJSONMap(src any) (map[string]any, error) {
249249
jsonData, err := json.Marshal(src)
250250
if err != nil {
251251
return nil, fmt.Errorf("failed to marshal struct to JSON: %w", err)
252252
}
253253

254254
// Then unmarshal the JSON to a map
255-
var result map[string]interface{}
255+
var result map[string]any
256256

257257
err = json.Unmarshal(jsonData, &result)
258258
if err != nil {
@@ -290,7 +290,7 @@ func StructToJSONMap(src interface{}) (map[string]interface{}, error) {
290290
//
291291
// Note: This function relies on the `omitempty` tag for fields that should be
292292
// removed when empty. Make sure your struct is properly tagged for the desired behavior.
293-
func SanitizeJSON(rawJSON string, template interface{}) ([]byte, error) {
293+
func SanitizeJSON(rawJSON string, template any) ([]byte, error) {
294294
// Unmarshal raw JSON into the provided struct template
295295
if err := json.Unmarshal([]byte(rawJSON), template); err != nil {
296296
return nil, fmt.Errorf("failed to unmarshal JSON: %w", err)

0 commit comments

Comments
 (0)