-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupath.go
123 lines (96 loc) · 2.14 KB
/
upath.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package upath
import (
"io/fs"
"strings"
"github.com/l4go/unifs"
)
type UPath struct {
p unifs.UniPath
}
var Zero = UPath{p: unifs.Zero}
func cast_upath_err(p unifs.UniPath, err error) (UPath, error) {
if err != nil {
return UPath{}, nil
}
return UPath{p: p}, err
}
func cast_upath(p unifs.UniPath) UPath {
return UPath{p: p}
}
func New(uni_name string) (UPath, error) {
return cast_upath_err(unifs.New(uni_name))
}
func MustNew(uni_name string) UPath {
return UPath{p: unifs.MustNew(uni_name)}
}
func NewByOS(os_name string) (UPath, error) {
return cast_upath_err(unifs.NewFromOSPath(os_name))
}
func MustNewByOS(os_name string) UPath {
return UPath{p: unifs.MustNewFromOSPath(os_name)}
}
func (up UPath) IsZero() bool {
return up.p.IsZero()
}
func (up UPath) String() string {
return up.p.String()
}
func (up UPath) FSPath() string {
return up.p.FSPath()
}
func FSPaths(ups []UPath) []string {
fs_names := make([]string, len(ups))
for i, up := range ups {
fs_names[i] = up.FSPath()
}
return fs_names
}
func (up UPath) Join(names ...string) (UPath, error) {
return cast_upath_err(up.p.Join(names...))
}
func (up UPath) MustJoin(names ...string) UPath {
return cast_upath(up.p.MustJoin(names...))
}
func (up UPath) Open(fsys fs.FS) (fs.File, error) {
return up.p.Open(fsys)
}
func (up UPath) Sub(fsys fs.FS) (fs.FS, error) {
return up.p.Sub(fsys)
}
func (up UPath) Stat(fsys fs.FS) (fs.FileInfo, error) {
return up.p.Stat(fsys)
}
func (up UPath) ReadFile(fsys fs.FS) ([]byte, error) {
return up.p.ReadFile(fsys)
}
func (up UPath) ReadDir(fsys fs.FS) ([]fs.DirEntry, error) {
return up.p.ReadDir(fsys)
}
func (up *UPath) UnmarshalTOML(decode func(interface{}) error) error {
var str string
if err := decode(&str); err != nil {
return err
}
var err error = nil
switch {
case str == "":
err = nil
*up = Zero
case strings.HasPrefix(str, "/"):
var new_up UPath
new_up, err = New(str)
if err == nil {
*up = new_up
}
default:
var new_up UPath
new_up, err = NewByOS(str)
if err == nil {
*up = new_up
}
}
return err
}
func (up *UPath) MarshalText() ([]byte, error) {
return []byte(up.String()), nil
}