@@ -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
192223func (p * FsPath ) Reader () (io.Reader , error ) {
193224 return p .fs .Open (p .absPath )
0 commit comments