-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex_test.go
81 lines (66 loc) · 1.47 KB
/
ex_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package recode_test
import (
"encoding/json"
"fmt"
"io"
"io/fs"
"os"
"github.com/l4go/recode"
)
type TextTextFile struct {
File string
TextFile TextFile `json:"-"`
}
func (ttf *TextTextFile) RebuildByType(fsys fs.FS) error {
f, err := fsys.Open(ttf.File)
if err != nil {
return &fs.PathError{Op: "RebuildByType", Path: ttf.File, Err: err}
}
defer f.Close()
dec := json.NewDecoder(f)
if err := dec.Decode(&ttf.TextFile); err != nil {
return &fs.PathError{Op: "RebuildByType", Path: ttf.File, Err: err}
}
return nil
}
type TextFile struct {
File string
Text string `json:"-"`
}
func (tf *TextFile) RebuildByType(fsys fs.FS) error {
f, err := fsys.Open(tf.File)
if err != nil {
return &fs.PathError{Op: "RebuildByType", Path: tf.File, Err: err}
}
defer f.Close()
text, err := io.ReadAll(f)
if err != nil {
return &fs.PathError{Op: "RebuildByType", Path: tf.File, Err: err}
}
tf.Text = string(text)
return nil
}
func Example() {
fsys := os.DirFS("testfs")
f, err := fsys.Open("testfile.json")
if err != nil {
fmt.Println("Error:", err)
return
}
defer f.Close()
dec := json.NewDecoder(f)
var ttf TextTextFile
if err := dec.Decode(&ttf); err != nil {
fmt.Println("Error:", err)
return
}
fmt.Printf("Before: %q\n", ttf.TextFile.Text)
if err := recode.RecursiveRebuild(&ttf, fsys); err != nil {
fmt.Println("Error:", err)
return
}
fmt.Printf("After: %q\n", ttf.TextFile.Text)
// Output:
// Before: ""
// After: "test text"
}