Skip to content

Commit

Permalink
refactor aliases to not be experted
Browse files Browse the repository at this point in the history
  • Loading branch information
mleku committed Dec 1, 2024
1 parent c44ec0b commit e49cf89
Show file tree
Hide file tree
Showing 409 changed files with 4,071 additions and 3,648 deletions.
12 changes: 6 additions & 6 deletions appdata/appdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@ import (

// GetDataDir returns an operating system specific directory to be used for storing application
// data for an application.
func GetDataDir(goos, appName string, roaming bool) string {
func GetDataDir(goos, appName st, roaming bo) st {
if appName == "" || appName == "." {
return "."
}
// The caller really shouldn't prepend the appName with a period, but if they do, handle it
// gracefully by trimming it.
appName = strings.TrimPrefix(appName, ".")
appNameUpper := string(unicode.ToUpper(rune(appName[0]))) + appName[1:]
appNameLower := string(unicode.ToLower(rune(appName[0]))) + appName[1:]
appNameUpper := st(unicode.ToUpper(rune(appName[0]))) + appName[1:]
appNameLower := st(unicode.ToLower(rune(appName[0]))) + appName[1:]
// Get the OS specific home directory via the Go standard lib.
var homeDir string
var homeDir st
var usr *user.User
var e error
var e er
if usr, e = user.Current(); chk.E(e) {
homeDir = usr.HomeDir
}
Expand Down Expand Up @@ -84,6 +84,6 @@ func GetDataDir(goos, appName string, roaming bool) string {
// Mac OS: $HOME/Library/Application Support/Myapp
// Windows: %LOCALAPPDATA%\Myapp
// Plan 9: $home/myapp
func Dir(appName string, roaming bool) string {
func Dir(appName st, roaming bo) st {
return GetDataDir(runtime.GOOS, appName, roaming)
}
16 changes: 8 additions & 8 deletions appdata/appdata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (
func TestAppDataDir(t *testing.T) {
// App name plus upper and lowercase variants.
appName := "myapp"
appNameUpper := string(unicode.ToUpper(rune(appName[0]))) + appName[1:]
appNameLower := string(unicode.ToLower(rune(appName[0]))) + appName[1:]
appNameUpper := st(unicode.ToUpper(rune(appName[0]))) + appName[1:]
appNameLower := st(unicode.ToLower(rune(appName[0]))) + appName[1:]
// When we're on Windows, set the expected local and roaming directories per
// the environment vars. When we aren't on Windows, the function should
// return the current directory when forced to provide the Windows path
Expand All @@ -32,7 +32,7 @@ func TestAppDataDir(t *testing.T) {
winRoaming = filepath.Join(roamingAppData, appNameUpper)
}
// Get the home directory to use for testing expected results.
var homeDir string
var homeDir st
usr, e := user.Current()
if e != nil {
t.Errorf("user.Current: %v", e)
Expand All @@ -43,10 +43,10 @@ func TestAppDataDir(t *testing.T) {
macAppData := filepath.Join(homeDir, "Library", "Application Support")
linuxConfigDir := filepath.Join(homeDir, ".config")
tests := []struct {
goos string
appName string
roaming bool
want string
goos st
appName st
roaming bo
want st
}{
// Various combinations of application name casing, leading period,
// operating system, and roaming flags.
Expand Down Expand Up @@ -126,6 +126,6 @@ func TestAppDataDir(t *testing.T) {

// TstAppDataDir makes the internal appDataDir function available to the test
// package.
func TstAppDataDir(goos, appName string, roaming bool) string {
func TstAppDataDir(goos, appName st, roaming bo) st {
return GetDataDir(goos, appName, roaming)
}
11 changes: 6 additions & 5 deletions appdata/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ import (
)

type (
B = []byte
S = string
E = error
N = int
Ctx = context.T
bo = bool
by = []byte
st = string
er = error
no = int
cx = context.T
)

var (
Expand Down
4 changes: 2 additions & 2 deletions apputil/apputil.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

// EnsureDir checks a file could be written to a path, creates the directories as needed
func EnsureDir(fileName string) {
func EnsureDir(fileName st) {
dirName := filepath.Dir(fileName)
if _, serr := os.Stat(dirName); serr != nil {
merr := os.MkdirAll(dirName, os.ModePerm)
Expand All @@ -17,7 +17,7 @@ func EnsureDir(fileName string) {
}

// FileExists reports whether the named file or directory exists.
func FileExists(filePath string) bool {
func FileExists(filePath st) bo {
_, e := os.Stat(filePath)
return e == nil
}
11 changes: 6 additions & 5 deletions apputil/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ import (
)

type (
B = []byte
S = string
E = error
N = int
Ctx = context.T
bo = bool
by = []byte
st = string
er = error
no = int
cx = context.T
)

var (
Expand Down
4 changes: 2 additions & 2 deletions atomic/assert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ import (
// with errors.As.
//
// assertErrorAsType(t, err, new(ErrFoo))
func assertErrorAsType(t *testing.T, err error, typ interface{}, msgAndArgs ...interface{}) bool {
func assertErrorAsType(t *testing.T, err er, typ interface{}, msgAndArgs ...interface{}) bo {
t.Helper()

return assert.True(t, errors.As(err, typ), msgAndArgs...)
}

func assertErrorJSONUnmarshalType(t *testing.T, err error, msgAndArgs ...interface{}) bool {
func assertErrorJSONUnmarshalType(t *testing.T, err er, msgAndArgs ...interface{}) bo {
t.Helper()

return assertErrorAsType(t, err, new(*json.UnmarshalTypeError), msgAndArgs...)
Expand Down
20 changes: 10 additions & 10 deletions atomic/bool.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ type Bool struct {
v Uint32
}

var _zeroBool bool
var _zeroBool bo

// NewBool creates a new Bool.
func NewBool(val bool) *Bool {
func NewBool(val bo) *Bool {
x := &Bool{}
if val != _zeroBool {
x.Store(val)
Expand All @@ -45,41 +45,41 @@ func NewBool(val bool) *Bool {
}

// Load atomically loads the wrapped bool.
func (x *Bool) Load() bool {
func (x *Bool) Load() bo {
return truthy(x.v.Load())
}

// Store atomically stores the passed bool.
func (x *Bool) Store(val bool) {
func (x *Bool) Store(val bo) {
x.v.Store(boolToInt(val))
}

// CAS is an atomic compare-and-swap for bool values.
//
// Deprecated: Use CompareAndSwap.
func (x *Bool) CAS(old, new bool) (swapped bool) {
func (x *Bool) CAS(old, new bo) (swapped bo) {
return x.CompareAndSwap(old, new)
}

// CompareAndSwap is an atomic compare-and-swap for bool values.
func (x *Bool) CompareAndSwap(old, new bool) (swapped bool) {
func (x *Bool) CompareAndSwap(old, new bo) (swapped bo) {
return x.v.CompareAndSwap(boolToInt(old), boolToInt(new))
}

// Swap atomically stores the given bool and returns the old
// value.
func (x *Bool) Swap(val bool) (old bool) {
func (x *Bool) Swap(val bo) (old bo) {
return truthy(x.v.Swap(boolToInt(val)))
}

// MarshalJSON encodes the wrapped bool into JSON.
func (x *Bool) MarshalJSON() ([]byte, error) {
func (x *Bool) MarshalJSON() (by, er) {
return json.Marshal(x.Load())
}

// UnmarshalJSON decodes a bool from JSON.
func (x *Bool) UnmarshalJSON(b []byte) error {
var v bool
func (x *Bool) UnmarshalJSON(b by) er {
var v bo
if err := json.Unmarshal(b, &v); err != nil {
return err
}
Expand Down
8 changes: 4 additions & 4 deletions atomic/bool_ext.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,19 @@ import (

//go:generate bin/gen-atomicwrapper -name=Bool -type=bool -wrapped=Uint32 -pack=boolToInt -unpack=truthy -cas -swap -json -file=bool.go

func truthy(n uint32) bool {
func truthy(n uint32) bo {
return n == 1
}

func boolToInt(b bool) uint32 {
func boolToInt(b bo) uint32 {
if b {
return 1
}
return 0
}

// Toggle atomically negates the Boolean and returns the previous value.
func (b *Bool) Toggle() (old bool) {
func (b *Bool) Toggle() (old bo) {
for {
old := b.Load()
if b.CAS(old, !old) {
Expand All @@ -48,6 +48,6 @@ func (b *Bool) Toggle() (old bool) {
}

// String encodes the wrapped value as a string.
func (b *Bool) String() string {
func (b *Bool) String() st {
return strconv.FormatBool(b.Load())
}
6 changes: 3 additions & 3 deletions atomic/bool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,17 @@ func TestBool(t *testing.T) {
atom.Store(true)
bytes, err := json.Marshal(atom)
require.NoError(t, err, "json.Marshal errored unexpectedly.")
require.Equal(t, []byte("true"), bytes, "json.Marshal encoded the wrong bytes.")
require.Equal(t, by("true"), bytes, "json.Marshal encoded the wrong bytes.")
})

t.Run("JSON/Unmarshal", func(t *testing.T) {
err := json.Unmarshal([]byte("false"), &atom)
err := json.Unmarshal(by("false"), &atom)
require.NoError(t, err, "json.Unmarshal errored unexpectedly.")
require.False(t, atom.Load(), "json.Unmarshal didn't set the correct value.")
})

t.Run("JSON/Unmarshal/Error", func(t *testing.T) {
err := json.Unmarshal([]byte("42"), &atom)
err := json.Unmarshal(by("42"), &atom)
require.Error(t, err, "json.Unmarshal didn't error as expected.")
assertErrorJSONUnmarshalType(t, err,
"json.Unmarshal failed with unexpected error %v, want UnmarshalTypeError.", err)
Expand Down
8 changes: 4 additions & 4 deletions atomic/duration.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ func (x *Duration) Store(val time.Duration) {
// CAS is an atomic compare-and-swap for time.Duration values.
//
// Deprecated: Use CompareAndSwap.
func (x *Duration) CAS(old, new time.Duration) (swapped bool) {
func (x *Duration) CAS(old, new time.Duration) (swapped bo) {
return x.CompareAndSwap(old, new)
}

// CompareAndSwap is an atomic compare-and-swap for time.Duration values.
func (x *Duration) CompareAndSwap(old, new time.Duration) (swapped bool) {
func (x *Duration) CompareAndSwap(old, new time.Duration) (swapped bo) {
return x.v.CompareAndSwap(int64(old), int64(new))
}

Expand All @@ -74,12 +74,12 @@ func (x *Duration) Swap(val time.Duration) (old time.Duration) {
}

// MarshalJSON encodes the wrapped time.Duration into JSON.
func (x *Duration) MarshalJSON() ([]byte, error) {
func (x *Duration) MarshalJSON() (by, er) {
return json.Marshal(x.Load())
}

// UnmarshalJSON decodes a time.Duration from JSON.
func (x *Duration) UnmarshalJSON(b []byte) error {
func (x *Duration) UnmarshalJSON(b by) er {
var v time.Duration
if err := json.Unmarshal(b, &v); err != nil {
return err
Expand Down
9 changes: 5 additions & 4 deletions atomic/duration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,18 @@ func TestDuration(t *testing.T) {
atom.Store(time.Second)
bytes, err := json.Marshal(atom)
require.NoError(t, err, "json.Marshal errored unexpectedly.")
require.Equal(t, []byte("1000000000"), bytes, "json.Marshal encoded the wrong bytes.")
require.Equal(t, by("1000000000"), bytes, "json.Marshal encoded the wrong bytes.")
})

t.Run("JSON/Unmarshal", func(t *testing.T) {
err := json.Unmarshal([]byte("1000000000"), &atom)
err := json.Unmarshal(by("1000000000"), &atom)
require.NoError(t, err, "json.Unmarshal errored unexpectedly.")
require.Equal(t, time.Second, atom.Load(), "json.Unmarshal didn't set the correct value.")
require.Equal(t, time.Second, atom.Load(),
"json.Unmarshal didn't set the correct value.")
})

t.Run("JSON/Unmarshal/Error", func(t *testing.T) {
err := json.Unmarshal([]byte("\"1000000000\""), &atom)
err := json.Unmarshal(by("\"1000000000\""), &atom)
require.Error(t, err, "json.Unmarshal didn't error as expected.")
assertErrorJSONUnmarshalType(t, err,
"json.Unmarshal failed with unexpected error %v, want UnmarshalTypeError.", err)
Expand Down
12 changes: 6 additions & 6 deletions atomic/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ type Error struct {
v Value
}

var _zeroError error
var _zeroError er

// NewError creates a new Error.
func NewError(val error) *Error {
func NewError(val er) *Error {
x := &Error{}
if val != _zeroError {
x.Store(val)
Expand All @@ -41,17 +41,17 @@ func NewError(val error) *Error {
}

// Load atomically loads the wrapped error.
func (x *Error) Load() error {
func (x *Error) Load() er {
return unpackError(x.v.Load())
}

// Store atomically stores the passed error.
func (x *Error) Store(val error) {
func (x *Error) Store(val er) {
x.v.Store(packError(val))
}

// CompareAndSwap is an atomic compare-and-swap for error values.
func (x *Error) CompareAndSwap(old, new error) (swapped bool) {
func (x *Error) CompareAndSwap(old, new er) (swapped bo) {
if x.v.CompareAndSwap(packError(old), packError(new)) {
return true
}
Expand All @@ -67,6 +67,6 @@ func (x *Error) CompareAndSwap(old, new error) (swapped bool) {

// Swap atomically stores the given error and returns the old
// value.
func (x *Error) Swap(val error) (old error) {
func (x *Error) Swap(val er) (old er) {
return unpackError(x.v.Swap(packError(val)))
}
6 changes: 3 additions & 3 deletions atomic/error_ext.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ package atomic

//go:generate bin/gen-atomicwrapper -name=Error -type=error -wrapped=Value -pack=packError -unpack=unpackError -compareandswap -swap -file=error.go

type packedError struct{ Value error }
type packedError struct{ Value er }

func packError(v error) interface{} {
func packError(v er) interface{} {
return packedError{v}
}

func unpackError(v interface{}) error {
func unpackError(v interface{}) er {
if err, ok := v.(packedError); ok {
return err.Value
}
Expand Down
4 changes: 2 additions & 2 deletions atomic/float32.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@ func (x *Float32) Swap(val float32) (old float32) {
}

// MarshalJSON encodes the wrapped float32 into JSON.
func (x *Float32) MarshalJSON() ([]byte, error) {
func (x *Float32) MarshalJSON() (by, er) {
return json.Marshal(x.Load())
}

// UnmarshalJSON decodes a float32 from JSON.
func (x *Float32) UnmarshalJSON(b []byte) error {
func (x *Float32) UnmarshalJSON(b by) er {
var v float32
if err := json.Unmarshal(b, &v); err != nil {
return err
Expand Down
Loading

0 comments on commit e49cf89

Please sign in to comment.