-
Notifications
You must be signed in to change notification settings - Fork 0
/
record.go
71 lines (61 loc) · 1.74 KB
/
record.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
package tar
import (
"path/filepath"
"strings"
A "github.com/IBM/fp-go/array"
B "github.com/IBM/fp-go/bytes"
F "github.com/IBM/fp-go/function"
O "github.com/IBM/fp-go/option"
RR "github.com/IBM/fp-go/record"
SG "github.com/IBM/fp-go/semigroup"
S "github.com/IBM/fp-go/string"
T "github.com/IBM/fp-go/tuple"
)
var (
// the empty file list
Empty = RR.Empty[string, []byte]()
// LookupBytes accesses a file as bytes from a file list
LookupBytes = F.Flow2(
cleanPath,
RR.Lookup[[]byte, string],
)
// the monoid used to merge FileList objects
Monoid = RR.UnionMonoid[string](SG.Last[[]byte]())
)
func cleanPath(path string) string {
return strings.Trim(filepath.ToSlash(filepath.Clean(path)), "/")
}
// UpsertBytes adds a file by path to a file list
func UpsertBytes(path string) func([]byte) func(FileList) FileList {
return F.Bind1st(RR.UpsertAt[string, []byte], cleanPath(path))
}
// UpsertString adds a file by path to a file list
func UpsertString(path string) func(string) func(FileList) FileList {
return F.Flow2(
S.ToBytes,
UpsertBytes(path),
)
}
// LookupString accesses a file as string from a file list
func LookupString(path string) func(FileList) O.Option[string] {
return F.Flow2(
LookupBytes(path),
O.Map(B.ToString),
)
}
// FromEntries creates a file list from a set of entries
func FromEntries(entries []FileEntry) FileList {
return RR.FromEntries(entries)
}
// CreateEntry constructs a file entry with byte content
func CreateEntry(path string) func([]byte) FileEntry {
return F.Bind1st(T.MakeTuple2[string, []byte], cleanPath(path))
}
// Singleton returns a file list with one single entry
func Singleton(path string) func([]byte) FileList {
return F.Flow3(
CreateEntry(path),
A.Of[FileEntry],
FromEntries,
)
}