Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat 17/cleanup #18

Merged
merged 15 commits into from
Feb 8, 2024
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions sql/db_test.go → database/sql/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
"fmt"
"testing"

"go.adoublef.dev/sdk/sql"
st "go.adoublef.dev/sdk/sql/sqltest"
"github.com/matryer/is"
"go.adoublef.dev/sdk/database/sql"
st "go.adoublef.dev/sdk/database/sql/sqltest"
)

//go:embed all:*.up.sql
Expand Down
2 changes: 1 addition & 1 deletion sql/sqltest/db.go → database/sql/sqltest/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"path/filepath"
"testing"

"go.adoublef.dev/sdk/sql"
"go.adoublef.dev/sdk/database/sql"
)

// RoundTrip will create a database instance to use with testing
Expand Down
54 changes: 1 addition & 53 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,64 +6,12 @@ require (
github.com/maragudk/migrate v0.4.3
github.com/matryer/is v1.4.1
github.com/mattn/go-sqlite3 v1.14.22
github.com/minio/minio-go/v7 v7.0.66
github.com/testcontainers/testcontainers-go v0.27.0
go.adoublef.dev/is v0.1.2
golang.org/x/sync v0.6.0
)

require (
dario.cat/mergo v1.0.0 // indirect
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/Microsoft/hcsshim v0.11.4 // indirect
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
github.com/containerd/containerd v1.7.11 // indirect
github.com/containerd/log v0.1.0 // indirect
github.com/cpuguy83/dockercfg v0.3.1 // indirect
github.com/docker/distribution v2.8.2+incompatible // indirect
github.com/docker/docker v24.0.7+incompatible // indirect
github.com/docker/go-connections v0.4.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/go-ole/go-ole v1.2.6 // indirect
github.com/go-sql-driver/mysql v1.7.1 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/uuid v1.5.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.17.4 // indirect
github.com/klauspost/cpuid/v2 v2.2.6 // indirect
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/minio/md5-simd v1.1.2 // indirect
github.com/minio/sha256-simd v1.0.1 // indirect
github.com/moby/patternmatcher v0.6.0 // indirect
github.com/moby/sys/sequential v0.5.0 // indirect
github.com/moby/term v0.5.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/morikuni/aec v1.0.0 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.1.0-rc5 // indirect
github.com/opencontainers/runc v1.1.5 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
github.com/rs/xid v1.5.0 // indirect
github.com/shirou/gopsutil/v3 v3.23.11 // indirect
github.com/shoenig/go-m1cpu v0.1.6 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/tklauser/go-sysconf v0.3.12 // indirect
github.com/tklauser/numcpus v0.6.1 // indirect
github.com/yusufpapurcu/wmi v1.2.3 // indirect
golang.org/x/crypto v0.16.0 // indirect
golang.org/x/exp v0.0.0-20230510235704-dd950f8aeaea // indirect
golang.org/x/mod v0.11.0 // indirect
golang.org/x/net v0.19.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/tools v0.10.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230711160842-782d3b101e98 // indirect
google.golang.org/grpc v1.58.3 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
)
188 changes: 2 additions & 186 deletions go.sum

Large diffs are not rendered by default.

87 changes: 87 additions & 0 deletions io/fs/path.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package fs

import (
"encoding/json"
"errors"
"fmt"
"path"
"strings"
)

const (
root = "/"
parent = ".."
)

// Path is the absolute pathname given to a file
type Path struct {
Dir string // dirname
Base string // filename
}

func (p Path) String() string {
return path.Join(p.Dir, p.Base)
}

func (p *Path) UnmarshalJSON(b []byte) error {
var s string
err := json.Unmarshal(b, &s)
if err != nil {
return fmt.Errorf("os: unable to decode: %w", err)
}
*p, err = ParsePath(s)
return err
}

func (p Path) MarshalJSON() ([]byte, error) {
return json.Marshal(p.String())
}

// ParsePath
func ParsePath(s string) (Path, error) {
if s == "" {
return Path{}, ErrPathLen0
}
if len(s) > 255 {
return Path{}, ErrPathLen255
}
if strings.Contains(s, parent) || !(strings.HasPrefix(s, root)) {
return Path{}, ErrPathAbs
}

dir, base := path.Split(s)
if base == "" {
return Path{}, ErrBaseLen0
}
return Path{Dir: dir, Base: base}, nil
}

// MustPath panics if an error occurs
func MustPath(s string) Path {
p, err := ParsePath(s)
if err != nil {
panic(err)
}
return p
}

func IsNil(p Path) bool {
return p.Base == ""
}

// Join is a wrapper for the stdlib path.Join function
func Join(elem ...string) string {
return path.Join(elem...)
}

// Dir returns all but the last element of path
func Dir(s string) string {
return path.Dir(s)
}

var (
ErrPathLen0 = errors.New("fs: pathname must be provided")
ErrPathLen255 = errors.New("fs: pathname exceeds 255 characters")
ErrPathAbs = errors.New("fs: absolute path not provided")
ErrBaseLen0 = errors.New("fs: basename must be provided")
)
103 changes: 103 additions & 0 deletions io/fs/path_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package fs_test

import (
"encoding/json"
"strings"
"testing"

"go.adoublef.dev/is"
. "go.adoublef.dev/sdk/io/fs"
)

func TestParsePath(t *testing.T) {
tt := map[string]struct {
s string
err error
}{
"ParsePath(\"/a/b/c.d\")": {
s: "/a/b/c.d",
},
"ErrPathAbs(\"./a/b/c\")": {
s: "./a/b/c",
err: ErrPathAbs,
},
"ErrPathAbs(\"~/a/b/c\")": {
s: "~/a/b/c",
err: ErrPathAbs,
},
"ErrPathLen0(\"\")": {
err: ErrPathLen0,
},
"ErrPathAbs(\"../..\")": {
s: "../..",
err: ErrPathAbs,
},
"ErrPathAbs(\"..\")": {
s: "..",
err: ErrPathAbs,
},
"ParsePath(\"/a\")": {
s: "/a",
},
"ErrPathLen255(\"/1/2/.../255/m\")": {
s: strings.Repeat("/n/", 255) + "m",
err: ErrPathLen255,
},
"ErrBaseLen0(\"/a/b/\")": {
s: "/a/b/",
err: ErrBaseLen0,
},
}

for name, tc := range tt {
t.Run(name, func(t *testing.T) {
is := is.NewRelaxed(t)

p, err := ParsePath(tc.s)
is.Err(err, tc.err) // parse path
t.Log(p)
})
}
}

func TestPath_UnmarshalJSON(t *testing.T) {
tt := map[string]struct {
s string
err error
}{
"OK": {
s: "\"/a.txt\"",
},
}

for name, tc := range tt {
t.Run(name, func(t *testing.T) {
var (
is = is.NewRelaxed(t)
)

var path Path
err := json.NewDecoder(strings.NewReader(tc.s)).Decode(&path)
is.Err(err, tc.err) // decode pathname
})
}
}

func TestPath_MarshalJSON(t *testing.T) {
t.Run("OK", func(t *testing.T) {
var (
is = is.NewRelaxed(t)
)
input := "\"/a.txt\""

var path Path
err := json.NewDecoder(strings.NewReader(input)).Decode(&path)
is.NoErr(err) // decode pathname

var sb strings.Builder
err = json.NewEncoder(&sb).Encode(path)
is.NoErr(err) // encode pathname

is.Equal(strings.TrimSpace(sb.String()), input)
})
}
27 changes: 27 additions & 0 deletions io/fs/size.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package fs

import "go.adoublef.dev/sdk/strconv"

// Size
type Size uint

const (
B Size = 1 << (10 * iota)
KB
MB
GB
)

// Int reutrns Size as an int primitive value.
func (s Size) Int() int {
return int(s)
}

func (s Size) String() string {
return strconv.Utoa(uint(s))
}

// IEC returns Size as a formated string using the IEC standard
func (s Size) IEC() string {
return strconv.FormatIEC(uint(s))
}
File renamed without changes.
15 changes: 3 additions & 12 deletions bytest/reader.go → io/iotest/reader.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,8 @@
// source https://medium.com/@nicksoetaert/using-io-reader-to-simulate-an-arbitrary-sized-file-in-golang-4df6287cae51
package bytest
package iotest

import (
"io"
)

const (
B = 1 << (10 * iota)
KB
MB
GB // 1048576000 1073741824
)
import "io"

// NewReader creates a read-only Reader of an arbitary size.
func NewReader(n int) *Reader {
return &Reader{n: n}
}
Expand Down
15 changes: 8 additions & 7 deletions bytest/reader_test.go → io/iotest/reader_test.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
package bytest_test
package iotest_test

import (
"io"
"testing"

"github.com/matryer/is"
"go.adoublef.dev/sdk/bytest"
"go.adoublef.dev/is"
"go.adoublef.dev/sdk/io/fs"
. "go.adoublef.dev/sdk/io/iotest"
)

func TestReader(t *testing.T) {
func TestReader_Read(t *testing.T) {
t.Parallel()

tt := []struct {
name string
r *bytest.Reader
r *Reader
n int
}{
{
name: "1024 Bytes",
r: bytest.NewReader(bytest.KB),
n: bytest.KB,
r: NewReader(fs.KB.Int()),
n: fs.KB.Int(),
},
}

Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions http/fs/fs_test.go → net/http/fs/fs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"embed"
"testing"

"github.com/matryer/is"
"go.adoublef.dev/sdk/http/fs"
"go.adoublef.dev/is"
"go.adoublef.dev/sdk/net/http/fs"
)

//go:embed all:testdata
Expand Down
File renamed without changes.
File renamed without changes.
26 changes: 0 additions & 26 deletions sql/julian/time.go

This file was deleted.

Loading
Loading