diff --git a/.golangci.yml b/.golangci.yml new file mode 100644 index 00000000..a295e582 --- /dev/null +++ b/.golangci.yml @@ -0,0 +1,6 @@ +# Options for analysis running. +linters-settings: + gocyclo: + # Minimal code complexity to report. + # Default: 30 (but we recommend 10-20) + min-complexity: 10 \ No newline at end of file diff --git a/Makefile b/Makefile index e04af228..d3dc4f55 100644 --- a/Makefile +++ b/Makefile @@ -49,3 +49,5 @@ lines: go_lines_count ./ ./docs/lines_count.txt 10 licenses: golicense -out-xlsx=./docs/licenses.xlsx $(FILEAPP) +gocyclo: + golangci-lint run ./... --disable-all -E gocyclo -v diff --git a/micro/microfunctions.go b/micro/microfunctions.go index a4fb12cd..06614763 100644 --- a/micro/microfunctions.go +++ b/micro/microfunctions.go @@ -978,3 +978,13 @@ func IsInt(s string) bool { Otvet = true return Otvet } + +// Int32FromString - возвращает int32 из строки +func Int32FromString(s string) (int64, error) { + var Otvet int64 + var err error + + Otvet, err = strconv.ParseInt(s, 10, 32) + + return Otvet, err +} diff --git a/micro/microfunctions_test.go b/micro/microfunctions_test.go index 521b8de3..f9b2ff7d 100644 --- a/micro/microfunctions_test.go +++ b/micro/microfunctions_test.go @@ -873,3 +873,38 @@ func TestIsInt(t *testing.T) { t.Errorf("Expected false for string containing non-digit characters, but got: %v", nonDigitResult) } } + +func TestInt32FromString(t *testing.T) { + // Test converting a valid string to int32 + input1 := "12345" + expected1 := int64(12345) + result1, err1 := Int32FromString(input1) + if err1 != nil { + t.Errorf("Expected no error, but got: %v", err1) + } + if result1 != expected1 { + t.Errorf("Expected %d, but got: %d", expected1, result1) + } + + // Test converting an empty string to int32 + input2 := "" + expected2 := int64(0) + result2, err2 := Int32FromString(input2) + if err2 == nil { + t.Errorf("Expected error, but got: %v", err2) + } + if result2 != expected2 { + t.Errorf("Expected %d, but got: %d", expected2, result2) + } + + // Test converting an invalid string to int32 + input3 := "abc" + expected3 := int64(0) + result3, err3 := Int32FromString(input3) + if err3 == nil { + t.Error("Expected an error, but got none") + } + if result3 != expected3 { + t.Errorf("Expected %d, but got: %d", expected3, result3) + } +}