-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
40 changed files
with
1,955 additions
and
120 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package env | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestEnv(t *testing.T) { | ||
assert.True(t, len(Env("any")) == 0) | ||
envLock.RLock() | ||
val, ok := envs["any"] | ||
envLock.RUnlock() | ||
assert.True(t, len(val) == 0) | ||
assert.True(t, ok) | ||
assert.True(t, len(Env("any")) == 0) | ||
} | ||
|
||
func TestEnvInt(t *testing.T) { | ||
val, ok := EnvInt("any") | ||
assert.Equal(t, 0, val) | ||
assert.False(t, ok) | ||
t.Setenv("anyInt", "10") | ||
val, ok = EnvInt("anyInt") | ||
assert.Equal(t, 10, val) | ||
assert.True(t, ok) | ||
t.Setenv("anyString", "a") | ||
val, ok = EnvInt("anyString") | ||
assert.Equal(t, 0, val) | ||
assert.False(t, ok) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package lang | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
"strconv" | ||
) | ||
|
||
// Placeholder is a placeholder object that can be used globally. | ||
var Placeholder PlaceholderType | ||
|
||
type ( | ||
// AnyType can be used to hold any type. | ||
AnyType = any | ||
// PlaceholderType represents a placeholder type. | ||
PlaceholderType = struct{} | ||
) | ||
|
||
// Repr returns the string representation of v. | ||
func Repr(v any) string { | ||
if v == nil { | ||
return "" | ||
} | ||
|
||
// if func (v *Type) String() string, we can't use Elem() | ||
switch vt := v.(type) { | ||
case fmt.Stringer: | ||
return vt.String() | ||
} | ||
|
||
val := reflect.ValueOf(v) | ||
for val.Kind() == reflect.Ptr && !val.IsNil() { | ||
val = val.Elem() | ||
} | ||
|
||
return reprOfValue(val) | ||
} | ||
|
||
func reprOfValue(val reflect.Value) string { | ||
switch vt := val.Interface().(type) { | ||
case bool: | ||
return strconv.FormatBool(vt) | ||
case error: | ||
return vt.Error() | ||
case float32: | ||
return strconv.FormatFloat(float64(vt), 'f', -1, 32) | ||
case float64: | ||
return strconv.FormatFloat(vt, 'f', -1, 64) | ||
case fmt.Stringer: | ||
return vt.String() | ||
case int: | ||
return strconv.Itoa(vt) | ||
case int8: | ||
return strconv.Itoa(int(vt)) | ||
case int16: | ||
return strconv.Itoa(int(vt)) | ||
case int32: | ||
return strconv.Itoa(int(vt)) | ||
case int64: | ||
return strconv.FormatInt(vt, 10) | ||
case string: | ||
return vt | ||
case uint: | ||
return strconv.FormatUint(uint64(vt), 10) | ||
case uint8: | ||
return strconv.FormatUint(uint64(vt), 10) | ||
case uint16: | ||
return strconv.FormatUint(uint64(vt), 10) | ||
case uint32: | ||
return strconv.FormatUint(uint64(vt), 10) | ||
case uint64: | ||
return strconv.FormatUint(vt, 10) | ||
case []byte: | ||
return string(vt) | ||
default: | ||
return fmt.Sprint(val.Interface()) | ||
} | ||
} |
Oops, something went wrong.