mirror of
https://github.com/Luzifer/rconfig.git
synced 2024-11-09 16:30:04 +00:00
Compare commits
No commits in common. "9a13c8c9f1ced3cd1402fd86c51332eab0662c9b" and "7572b1fd7b890283159b1be9e499b0e4122a2364" have entirely different histories.
9a13c8c9f1
...
7572b1fd7b
4 changed files with 14 additions and 76 deletions
|
@ -31,11 +31,11 @@ linters:
|
|||
- bodyclose # checks whether HTTP response body is closed successfully [fast: true, auto-fix: false]
|
||||
- containedctx # containedctx is a linter that detects struct contained context.Context field [fast: true, auto-fix: false]
|
||||
- contextcheck # check the function whether use a non-inherited context [fast: false, auto-fix: false]
|
||||
- copyloopvar # copyloopvar is a linter detects places where loop variables are copied [fast: true, auto-fix: false]
|
||||
- dogsled # Checks assignments with too many blank identifiers (e.g. x, _, _, _, := f()) [fast: true, auto-fix: false]
|
||||
- durationcheck # check for two durations multiplied together [fast: false, auto-fix: false]
|
||||
- errcheck # Errcheck is a program for checking for unchecked errors in go programs. These unchecked errors can be critical bugs in some cases [fast: false, auto-fix: false]
|
||||
- errchkjson # Checks types passed to the json encoding functions. Reports unsupported types and optionally reports occations, where the check for the returned error can be omitted. [fast: false, auto-fix: false]
|
||||
- exportloopref # checks for pointers to enclosing loop variables [fast: true, auto-fix: false]
|
||||
- forbidigo # Forbids identifiers [fast: true, auto-fix: false]
|
||||
- funlen # Tool for detection of long functions [fast: true, auto-fix: false]
|
||||
- gocognit # Computes and checks the cognitive complexity of functions [fast: true, auto-fix: false]
|
||||
|
|
32
config.go
32
config.go
|
@ -258,8 +258,8 @@ func execTags(in interface{}, fs *pflag.FlagSet) ([]afterFunc, error) {
|
|||
valField.SetBool(v)
|
||||
}
|
||||
|
||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||
vt, err := parseIntForType(value, 10, typeField.Type.Kind()) //nolint:mnd
|
||||
case reflect.Int, reflect.Int8, reflect.Int32, reflect.Int64:
|
||||
vt, err := strconv.ParseInt(value, 10, 64)
|
||||
if err != nil {
|
||||
if value != "" {
|
||||
return nil, fmt.Errorf("parsing int: %w", err)
|
||||
|
@ -273,7 +273,7 @@ func execTags(in interface{}, fs *pflag.FlagSet) ([]afterFunc, error) {
|
|||
}
|
||||
|
||||
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
||||
vt, err := parseUintForType(value, 10, typeField.Type.Kind()) //nolint:mnd
|
||||
vt, err := strconv.ParseUint(value, 10, 64)
|
||||
if err != nil {
|
||||
if value != "" {
|
||||
return nil, fmt.Errorf("parsing uint: %w", err)
|
||||
|
@ -371,21 +371,15 @@ func registerFlagInt(t reflect.Kind, fs *pflag.FlagSet, field interface{}, parts
|
|||
}
|
||||
case reflect.Int8:
|
||||
if len(parts) == 1 {
|
||||
fs.Int8Var(field.(*int8), parts[0], int8(vt), desc) //#nosec:G115 - Variable is guaranteed to match by strconv
|
||||
fs.Int8Var(field.(*int8), parts[0], int8(vt), desc)
|
||||
} else {
|
||||
fs.Int8VarP(field.(*int8), parts[0], parts[1], int8(vt), desc) //#nosec:G115 - Variable is guaranteed to match by strconv
|
||||
}
|
||||
case reflect.Int16:
|
||||
if len(parts) == 1 {
|
||||
fs.Int16Var(field.(*int16), parts[0], int16(vt), desc) //#nosec:G115 - Variable is guaranteed to match by strconv
|
||||
} else {
|
||||
fs.Int16VarP(field.(*int16), parts[0], parts[1], int16(vt), desc) //#nosec:G115 - Variable is guaranteed to match by strconv
|
||||
fs.Int8VarP(field.(*int8), parts[0], parts[1], int8(vt), desc)
|
||||
}
|
||||
case reflect.Int32:
|
||||
if len(parts) == 1 {
|
||||
fs.Int32Var(field.(*int32), parts[0], int32(vt), desc) //#nosec:G115 - Variable is guaranteed to match by strconv
|
||||
fs.Int32Var(field.(*int32), parts[0], int32(vt), desc)
|
||||
} else {
|
||||
fs.Int32VarP(field.(*int32), parts[0], parts[1], int32(vt), desc) //#nosec:G115 - Variable is guaranteed to match by strconv
|
||||
fs.Int32VarP(field.(*int32), parts[0], parts[1], int32(vt), desc)
|
||||
}
|
||||
case reflect.Int64:
|
||||
if len(parts) == 1 {
|
||||
|
@ -406,21 +400,21 @@ func registerFlagUint(t reflect.Kind, fs *pflag.FlagSet, field interface{}, part
|
|||
}
|
||||
case reflect.Uint8:
|
||||
if len(parts) == 1 {
|
||||
fs.Uint8Var(field.(*uint8), parts[0], uint8(vt), desc) //#nosec:G115 - Variable is guaranteed to match by strconv
|
||||
fs.Uint8Var(field.(*uint8), parts[0], uint8(vt), desc)
|
||||
} else {
|
||||
fs.Uint8VarP(field.(*uint8), parts[0], parts[1], uint8(vt), desc) //#nosec:G115 - Variable is guaranteed to match by strconv
|
||||
fs.Uint8VarP(field.(*uint8), parts[0], parts[1], uint8(vt), desc)
|
||||
}
|
||||
case reflect.Uint16:
|
||||
if len(parts) == 1 {
|
||||
fs.Uint16Var(field.(*uint16), parts[0], uint16(vt), desc) //#nosec:G115 - Variable is guaranteed to match by strconv
|
||||
fs.Uint16Var(field.(*uint16), parts[0], uint16(vt), desc)
|
||||
} else {
|
||||
fs.Uint16VarP(field.(*uint16), parts[0], parts[1], uint16(vt), desc) //#nosec:G115 - Variable is guaranteed to match by strconv
|
||||
fs.Uint16VarP(field.(*uint16), parts[0], parts[1], uint16(vt), desc)
|
||||
}
|
||||
case reflect.Uint32:
|
||||
if len(parts) == 1 {
|
||||
fs.Uint32Var(field.(*uint32), parts[0], uint32(vt), desc) //#nosec:G115 - Variable is guaranteed to match by strconv
|
||||
fs.Uint32Var(field.(*uint32), parts[0], uint32(vt), desc)
|
||||
} else {
|
||||
fs.Uint32VarP(field.(*uint32), parts[0], parts[1], uint32(vt), desc) //#nosec:G115 - Variable is guaranteed to match by strconv
|
||||
fs.Uint32VarP(field.(*uint32), parts[0], parts[1], uint32(vt), desc)
|
||||
}
|
||||
case reflect.Uint64:
|
||||
if len(parts) == 1 {
|
||||
|
|
|
@ -9,7 +9,6 @@ func TestIntParsing(t *testing.T) {
|
|||
args = []string{
|
||||
"--int=1", "-i", "2",
|
||||
"--int8=3", "-8", "4",
|
||||
"--int16=2", "-1", "3",
|
||||
"--int32=5", "-3", "6",
|
||||
"--int64=7", "-6", "8",
|
||||
}
|
||||
|
@ -18,8 +17,6 @@ func TestIntParsing(t *testing.T) {
|
|||
TestP int `flag:"intp,i"`
|
||||
Test8 int8 `flag:"int8"`
|
||||
Test8P int8 `flag:"int8p,8"`
|
||||
Test16 int16 `flag:"int16"`
|
||||
Test16P int16 `flag:"int16p,1"`
|
||||
Test32 int32 `flag:"int32"`
|
||||
Test32P int32 `flag:"int32p,3"`
|
||||
Test64 int64 `flag:"int64"`
|
||||
|
@ -37,8 +34,6 @@ func TestIntParsing(t *testing.T) {
|
|||
{cfg.TestP, 2},
|
||||
{cfg.Test8, int8(3)},
|
||||
{cfg.Test8P, int8(4)},
|
||||
{cfg.Test16, int16(2)},
|
||||
{cfg.Test16P, int16(3)},
|
||||
{cfg.Test32, int32(5)},
|
||||
{cfg.Test32P, int32(6)},
|
||||
{cfg.Test64, int64(7)},
|
||||
|
|
51
strconv.go
51
strconv.go
|
@ -1,51 +0,0 @@
|
|||
package rconfig
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func parseIntForType(s string, base int, fieldType reflect.Kind) (i int64, err error) {
|
||||
switch fieldType {
|
||||
case reflect.Int:
|
||||
return strconv.ParseInt(s, base, strconv.IntSize) //nolint:wrapcheck
|
||||
|
||||
case reflect.Int8:
|
||||
return strconv.ParseInt(s, base, 8) //nolint:wrapcheck
|
||||
|
||||
case reflect.Int16:
|
||||
return strconv.ParseInt(s, base, 16) //nolint:wrapcheck
|
||||
|
||||
case reflect.Int32:
|
||||
return strconv.ParseInt(s, base, 32) //nolint:wrapcheck
|
||||
|
||||
case reflect.Int64:
|
||||
return strconv.ParseInt(s, base, 64) //nolint:wrapcheck
|
||||
|
||||
default:
|
||||
return 0, fmt.Errorf("unsupported type: %v", fieldType)
|
||||
}
|
||||
}
|
||||
|
||||
func parseUintForType(s string, base int, fieldType reflect.Kind) (uint64, error) {
|
||||
switch fieldType {
|
||||
case reflect.Uint:
|
||||
return strconv.ParseUint(s, base, strconv.IntSize) //nolint:wrapcheck
|
||||
|
||||
case reflect.Uint8:
|
||||
return strconv.ParseUint(s, base, 8) //nolint:wrapcheck
|
||||
|
||||
case reflect.Uint16:
|
||||
return strconv.ParseUint(s, base, 16) //nolint:wrapcheck
|
||||
|
||||
case reflect.Uint32:
|
||||
return strconv.ParseUint(s, base, 32) //nolint:wrapcheck
|
||||
|
||||
case reflect.Uint64:
|
||||
return strconv.ParseUint(s, base, 64) //nolint:wrapcheck
|
||||
|
||||
default:
|
||||
return 0, fmt.Errorf("unsupported type: %v", fieldType)
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue