Skip to content

Commit

Permalink
⚙️ refactor: Refactor env handling
Browse files Browse the repository at this point in the history
  • Loading branch information
sohaha committed Nov 14, 2024
1 parent c540167 commit 9194c7e
Show file tree
Hide file tree
Showing 8 changed files with 158 additions and 80 deletions.
45 changes: 0 additions & 45 deletions zarray/slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ package zarray

import (
"math/rand"
"strings"

"github.com/sohaha/zlsgo/zstring"
"github.com/sohaha/zlsgo/zsync"
"github.com/sohaha/zlsgo/ztype"
"github.com/sohaha/zlsgo/zutil"
)

Expand Down Expand Up @@ -197,46 +195,3 @@ func Shift[T comparable](list *[]T) (v T) {
*list = (*list)[1:]
return
}

// Slice converts a string to a slice.
// If n is not empty, the string will be split into n parts.
func Slice[T comparable](s string, n ...int) []T {
if s == "" {
return []T{}
}

var ss []string
if len(n) > 0 {
ss = strings.SplitN(s, ",", n[0])
} else {
ss = strings.Split(s, ",")
}
res := make([]T, len(ss))
for i := range ss {
ztype.To(zstring.TrimSpace(ss[i]), &res[i])
}

return res
}

// Join slice to string.
// If n is not empty, the string will be split into n parts.
func Join[T comparable](s []T, sep string) string {
if len(s) == 0 {
return ""
}

b := zstring.Buffer(len(s))
for i := 0; i < len(s); i++ {
v := ztype.ToString(s[i])
if v == "" {
continue
}
b.WriteString(v)
if i < len(s)-1 {
b.WriteString(sep)
}
}

return b.String()
}
19 changes: 0 additions & 19 deletions zarray/slice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,22 +130,3 @@ func TestFind(t *testing.T) {
tt.EqualTrue(!ok)
tt.Equal("", v["name"])
}

func TestSlice(t *testing.T) {
tt := zlsgo.NewTest(t)
tt.Equal([]string{"a", "b", "c"}, zarray.Slice[string]("a,b,c"))
tt.Equal([]int{1, 2, 3}, zarray.Slice[int]("1,2,3"))
tt.Equal([]float64{1.1, 2.2, 3.3}, zarray.Slice[float64]("1.1,2.2,3.3"))
tt.Equal([]string{"1.1", "2.2,3.3"}, zarray.Slice[string]("1.1,2.2,3.3", 2))
tt.Equal([]int{}, zarray.Slice[int](""))
}

func TestJoin(t *testing.T) {
tt := zlsgo.NewTest(t)
tt.Equal("a,b,c", zarray.Join([]string{"a", "b", "c"}, ","))
tt.Equal("1,2,3", zarray.Join([]int{1, 2, 3}, ","))
tt.Equal("1.1,2.2,3.3", zarray.Join([]float64{1.1, 2.2, 3.3}, ","))
tt.Equal("1.1,2.2,3.3", zarray.Join([]string{"1.1", "2.2", "3.3"}, ","))
tt.Equal("1.1,3.3", zarray.Join([]string{"1.1", "", "3.3"}, ","))
tt.Equal("", zarray.Join([]string{}, ","))
}
51 changes: 51 additions & 0 deletions zarray/string.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package zarray

import (
"strings"

"github.com/sohaha/zlsgo/zstring"
"github.com/sohaha/zlsgo/ztype"
)

// Slice converts a string to a slice.
// If n is not empty, the string will be split into n parts.
func Slice[T comparable](s string, n ...int) []T {
if s == "" {
return []T{}
}

var ss []string
if len(n) > 0 {
ss = strings.SplitN(s, ",", n[0])
} else {
ss = strings.Split(s, ",")
}
res := make([]T, len(ss))
for i := range ss {
ztype.To(zstring.TrimSpace(ss[i]), &res[i])
}

return res
}

// Join slice to string.
// If n is not empty, the string will be split into n parts.
func Join[T comparable](s []T, sep string) string {
if len(s) == 0 {
return ""
}

b := zstring.Buffer(len(s))
for i := 0; i < len(s); i++ {
v := ztype.ToString(s[i])
if v == "" {
continue
}
b.WriteString(v)
if i < len(s)-1 {
b.WriteString(sep)
}
}

return b.String()
}
30 changes: 30 additions & 0 deletions zarray/string_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//go:build go1.18
// +build go1.18

package zarray_test

import (
"testing"

"github.com/sohaha/zlsgo"
"github.com/sohaha/zlsgo/zarray"
)

func TestSlice(t *testing.T) {
tt := zlsgo.NewTest(t)
tt.Equal([]string{"a", "b", "c"}, zarray.Slice[string]("a,b,c"))
tt.Equal([]int{1, 2, 3}, zarray.Slice[int]("1,2,3"))
tt.Equal([]float64{1.1, 2.2, 3.3}, zarray.Slice[float64]("1.1,2.2,3.3"))
tt.Equal([]string{"1.1", "2.2,3.3"}, zarray.Slice[string]("1.1,2.2,3.3", 2))
tt.Equal([]int{}, zarray.Slice[int](""))
}

func TestJoin(t *testing.T) {
tt := zlsgo.NewTest(t)
tt.Equal("a,b,c", zarray.Join([]string{"a", "b", "c"}, ","))
tt.Equal("1,2,3", zarray.Join([]int{1, 2, 3}, ","))
tt.Equal("1.1,2.2,3.3", zarray.Join([]float64{1.1, 2.2, 3.3}, ","))
tt.Equal("1.1,2.2,3.3", zarray.Join([]string{"1.1", "2.2", "3.3"}, ","))
tt.Equal("1.1,3.3", zarray.Join([]string{"1.1", "", "3.3"}, ","))
tt.Equal("", zarray.Join([]string{}, ","))
}
14 changes: 14 additions & 0 deletions zstring/s_109.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//go:build !go1.10
// +build !go1.10

package zstring

import (
"bytes"
)

// Buffer Buffer
func Buffer(size ...int) *bytes.Buffer {
b := bytes.NewBufferString("")
return &b
}
12 changes: 2 additions & 10 deletions zstring/s_118.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
//go:build !go1.10
// +build !go1.10
//go:build go1.18
// +build go1.18

package zstring

import "bytes"

// Buffer Buffer
func Buffer(size ...int) *bytes.Buffer {
b := bytes.NewBufferString("")
return &b
}
62 changes: 57 additions & 5 deletions zutil/env.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package zutil

import (
"bytes"
"errors"
"fmt"
"os"
"runtime"
"strings"
"unicode"

"github.com/sohaha/zlsgo/zfile"
"github.com/sohaha/zlsgo/zstring"
Expand Down Expand Up @@ -37,10 +41,12 @@ func Getenv(name string, def ...string) string {
return val
}

// GOROOT return go root path
func GOROOT() string {
return zfile.RealPath(runtime.GOROOT())
}

// Loadenv load env from file
func Loadenv(filenames ...string) (err error) {
filenames = filenamesOrDefault(filenames)

Expand All @@ -60,15 +66,61 @@ func filenamesOrDefault(filenames []string) []string {
return filenames
}

func locateKeyName(src []byte) (key string, cutset []byte, err error) {
src = bytes.TrimLeftFunc(src, isSpace)
offset := 0
loop:
for i, char := range src {
rchar := rune(char)
if isSpace(rchar) {
continue
}

switch char {
case '=', ':':
key = string(src[0:i])
offset = i + 1
break loop
case '_':
default:
if unicode.IsLetter(rchar) || unicode.IsNumber(rchar) || rchar == '.' {
continue
}

return "", nil, fmt.Errorf(`unexpected character %q in variable name near %q`, char, src)
}
}

if len(src) == 0 {
return "", nil, errors.New("zero length string")
}

key = strings.TrimRightFunc(key, unicode.IsSpace)
cutset = bytes.TrimLeftFunc(src[offset:], isSpace)
return key, cutset, nil
}

func isSpace(r rune) bool {
switch r {
case '\t', '\v', '\f', '\r', ' ', 0x85, 0xA0:
return true
}
return false
}

func loadFile(filename string) error {
return zfile.ReadLineFile(filename, func(line int, data []byte) error {
e := strings.Split(zstring.Bytes2String(data), "=")
var value string
if len(e) > 1 {
value = e[1]
key, value, err := locateKeyName(data)
if err != nil {
return nil
}

value = bytes.TrimSpace(value)
if len(value) > 0 && (value[0] == '"' || value[0] == '\'' && value[0] == value[len(value)-1]) {
value = value[1 : len(value)-1]
}

_ = os.Setenv(zstring.TrimSpace(e[0]), strings.TrimSpace(value))
_ = os.Setenv(key, zstring.Bytes2String(value))
return nil
})
}
5 changes: 4 additions & 1 deletion zutil/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,14 @@ func TestGOROOT(t *testing.T) {

func TestLoadenv(t *testing.T) {
tt := zlsgo.NewTest(t)
_ = zfile.WriteFile(".env", []byte("myos=linux\n name=zls "))
_ = zfile.WriteFile(".env", []byte("myos=linux\n name=zls \n\n time=\"2024-11-14 23:59:01\" \n#comment='comment'\n description=\"hello world\""))
defer zfile.Rmdir(".env")

tt.NoError(zutil.Loadenv())

tt.Equal("linux", zutil.Getenv("myos"))
tt.Equal("zls", zutil.Getenv("name"))
tt.Equal("2024-11-14 23:59:01", zutil.Getenv("time"))
tt.Equal("", zutil.Getenv("comment"))
tt.Equal("hello world", zutil.Getenv("description"))
}

0 comments on commit 9194c7e

Please sign in to comment.