Skip to content

Commit 9a713f0

Browse files
committed
txtar: alias types to x/tools
Although we can't quite use the x/tools/txtar implementation in its entirety (golang/go#59264 needs to be fixed first, and we implement some other functions that x/tools/txtar doesn't (yet?) have, we can at least alias its types, which makes it possible to work with other packages that use those types.
1 parent a354da8 commit 9a713f0

File tree

4 files changed

+15
-17
lines changed

4 files changed

+15
-17
lines changed

go.mod

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@ module github.com/rogpeppe/go-internal
22

33
go 1.19
44

5-
require golang.org/x/mod v0.9.0
5+
require (
6+
golang.org/x/mod v0.9.0
7+
golang.org/x/tools v0.1.12
8+
)

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
golang.org/x/mod v0.9.0 h1:KENHtAZL2y3NLMYZeHY9DW8HW8V+kQyJsY/V9JlKvCs=
22
golang.org/x/mod v0.9.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
3+
golang.org/x/tools v0.1.12 h1:VveCTK38A2rkS8ZqFY25HIDFscX5X9OoEhJd3quQmXU=
4+
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=

txtar/archive.go

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -40,32 +40,22 @@ import (
4040
"path/filepath"
4141
"strings"
4242
"unicode/utf8"
43+
44+
"golang.org/x/tools/txtar"
4345
)
4446

4547
// An Archive is a collection of files.
46-
type Archive struct {
47-
Comment []byte
48-
Files []File
49-
}
48+
type Archive = txtar.Archive
5049

5150
// A File is a single file in an archive.
52-
type File struct {
53-
Name string // name of file ("foo/bar.txt")
54-
Data []byte // text content of file
55-
}
51+
type File = txtar.File
5652

5753
// Format returns the serialized form of an Archive.
5854
// It is assumed that the Archive data structure is well-formed:
5955
// a.Comment and all a.File[i].Data contain no file marker lines,
6056
// and all a.File[i].Name is non-empty.
6157
func Format(a *Archive) []byte {
62-
var buf bytes.Buffer
63-
buf.Write(fixNL(a.Comment))
64-
for _, f := range a.Files {
65-
fmt.Fprintf(&buf, "-- %s --\n", f.Name)
66-
buf.Write(fixNL(f.Data))
67-
}
68-
return buf.Bytes()
58+
return txtar.Format(a)
6959
}
7060

7161
// ParseFile parses the named file as an archive.
@@ -79,6 +69,9 @@ func ParseFile(file string) (*Archive, error) {
7969

8070
// Parse parses the serialized form of an Archive.
8171
// The returned Archive holds slices of data.
72+
//
73+
// TODO use golang.org/x/tools/txtar.Parse when https://github.com/golang/go/issues/59264
74+
// is fixed.
8275
func Parse(data []byte) *Archive {
8376
a := new(Archive)
8477
var name string

txtar/archive_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ hello world`,
4444
},
4545
// Test CRLF input
4646
{
47-
name: "basic",
47+
name: "basicCRLF",
4848
text: "blah\r\n-- hello --\r\nhello\r\n",
4949
parsed: &Archive{
5050
Comment: []byte("blah\r\n"),

0 commit comments

Comments
 (0)