diff --git a/.DS_Store b/.DS_Store index 634d4d473f7934c869c9ebc383a9e0bd2d463931..79f262438848dcd071097fbbc6fb8af739044c11 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/address_utils/address_utils.go b/address_utils/address_utils.go index 93779c7a2b060853f954b8be017b140a68198e63..e8a197bc4913199f6e9fe13d04de16413925a995 100644 --- a/address_utils/address_utils.go +++ b/address_utils/address_utils.go @@ -6,7 +6,7 @@ import ( "regexp" "strings" - "gitlab.com/uafrica/go-utils/string_utils" + "gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/string_utils" ) type Province struct { diff --git a/api_documentation/api_documentation.go b/api_documentation/api_documentation.go index 6468275ec9af787858309091e7d1a7d3e68a4fa2..2322c365ad86ebf31461b634923ca13b4bd8c5d9 100644 --- a/api_documentation/api_documentation.go +++ b/api_documentation/api_documentation.go @@ -2,8 +2,8 @@ package api_documentation import ( "fmt" - "gitlab.com/uafrica/go-utils/logs" - "gitlab.com/uafrica/go-utils/string_utils" + "gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/logs" + "gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/string_utils" "go/doc" "go/parser" "go/token" @@ -13,8 +13,8 @@ import ( "runtime" "strings" - "gitlab.com/uafrica/go-utils/errors" - "gitlab.com/uafrica/go-utils/handler_utils" + "gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/errors" + "gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/handler_utils" ) type NoParams struct{} @@ -323,10 +323,10 @@ func StructSchema(docs *Docs, t reflect.Type) (interface{}, error) { var err error fieldDesc := f.Tag.Get("doc") - if fieldDesc == "-" { + if fieldDesc == "-" { continue } - if fieldDesc == "" { + if fieldDesc == "" { fieldDesc = description + "." + fieldName } properties[fieldName], err = StructSchema(docs, f.Type) diff --git a/api_logs/api-logs.go b/api_logs/api-logs.go index 0e853e9aba747697a91f34b5b6ac261c741260cd..5abaefe07ec12f953bccd06812334e3ff54baa3f 100644 --- a/api_logs/api-logs.go +++ b/api_logs/api-logs.go @@ -2,7 +2,7 @@ package api_logs import ( "github.com/thoas/go-funk" - "gitlab.com/uafrica/go-utils/string_utils" + "gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/string_utils" "net/url" "strings" "time" diff --git a/api_responses/api_responses.go b/api_responses/api_responses.go index 373affea28cf66e68962c74237e008bb08442221..4774cc3513f5ad0a641487362475a99afc653340 100644 --- a/api_responses/api_responses.go +++ b/api_responses/api_responses.go @@ -9,10 +9,10 @@ import ( "strconv" "strings" - "gitlab.com/uafrica/go-utils/utils" + "gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/utils" - "gitlab.com/uafrica/go-utils/logs" - "gitlab.com/uafrica/go-utils/responses" + "gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/logs" + "gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/responses" "github.com/go-pg/pg/v10" @@ -89,7 +89,7 @@ func DatabaseServerErrorNew(err error, msg string) error { } } -//implements error so that API handler can extract the msg +// implements error so that API handler can extract the msg type ServerErrorStruct struct { error Message string diff --git a/audit/audit.go b/audit/audit.go index 2b466673a235669d3a5488cd004396484ca4f54e..19307cc00b888ea5616db89f970d909c2da5f5f4 100644 --- a/audit/audit.go +++ b/audit/audit.go @@ -8,8 +8,8 @@ import ( "strings" "github.com/r3labs/diff/v2" - "gitlab.com/uafrica/go-utils/reflection" - "gitlab.com/uafrica/go-utils/string_utils" + "gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/reflection" + "gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/string_utils" ) type FieldChange struct { diff --git a/cognito/cognito.go b/cognito/cognito.go index afae2c91b5821bf879cac0ef7fc9d1d07087be1c..d8300aee61cb523bc6fd133bda6fc39e9d31dfc2 100644 --- a/cognito/cognito.go +++ b/cognito/cognito.go @@ -5,7 +5,7 @@ import ( "math/rand" "strings" - "gitlab.com/uafrica/go-utils/logs" + "gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/logs" "github.com/aws/aws-lambda-go/events" "github.com/aws/aws-sdk-go/service/cognitoidentityprovider" diff --git a/errors/error_formats_test.go b/errors/error_formats_test.go index 57ac758e00a80a2d5b888cf4310bdaf027f1c886..3fa2330f62da429dec30fff37cb3f2a0b68e7a40 100644 --- a/errors/error_formats_test.go +++ b/errors/error_formats_test.go @@ -9,10 +9,10 @@ import ( pkg_errors "github.com/pkg/errors" - uf_errors "gitlab.com/uafrica/go-utils/errors" + uf_errors "gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/errors" ) -//use this struct to define various types of errors to experiment with +// use this struct to define various types of errors to experiment with type Test struct { title string err error @@ -20,57 +20,57 @@ type Test struct { var tests []Test -//define the errors for testing -//in each case, the main error message is "<this is broken>" -//in some cases that is passed up and wrapped with more messages -//the tests use different combinations of go errors, fmt.Errorf(), github.com/pkg/errors and our own go-utils/errors library -//NOTE: I put all the new calls on new lines to report different line numbers in the stack... +// define the errors for testing +// in each case, the main error message is "<this is broken>" +// in some cases that is passed up and wrapped with more messages +// the tests use different combinations of go errors, fmt.Errorf(), github.com/pkg/errors and our own go-utils/errors library +// NOTE: I put all the new calls on new lines to report different line numbers in the stack... func init() { tests = []Test{ - //a very simple go "errors.New()" error - does not have a stack + // a very simple go "errors.New()" error - does not have a stack {title: "go.New(...)", err: go_errors.New("<this is broken>")}, - //a fmt.Errorf() error - does not have a stack + // a fmt.Errorf() error - does not have a stack {title: "fmt.Errorf(...)", err: fmt.Errorf("<this is broken>")}, - //a better github.com/pkg/errors.New() error that includes the call stack + // a better github.com/pkg/errors.New() error that includes the call stack {title: "pkg.New(...)", err: pkg_errors.New("<this is broken>")}, - //our own error type that always records the caller: + // our own error type that always records the caller: {title: "uafrica.New(...)", err: uf_errors.New("<this is broken>")}, - //one can use pkg WithStack() to add a stack to an error that does not have a stack + // one can use pkg WithStack() to add a stack to an error that does not have a stack {title: "pkg.WithStack(go.New(...))", err: pkg_errors.WithStack( go_errors.New("<this is broken>"), )}, - //if you are ok with pkg's way to add a stack manually (I am not), - //then you could easily make this mistake to add a stack to an error that already has a stack - //by default pkg then prints two stacks and the rest of the error messages are after the last stack - //making it very hard to figure out what went wrong and where... + // if you are ok with pkg's way to add a stack manually (I am not), + // then you could easily make this mistake to add a stack to an error that already has a stack + // by default pkg then prints two stacks and the rest of the error messages are after the last stack + // making it very hard to figure out what went wrong and where... {title: "pkg.WithStack(pkg.New())", err: pkg_errors.WithStack( pkg_errors.New("<this is broken>"), )}, - //one can also add a stack with our own errors, - //note that we always want an explanation which is just to enforce good practice: + // one can also add a stack with our own errors, + // note that we always want an explanation which is just to enforce good practice: {title: "uafrica.Wrapf(go.New(...))", err: uf_errors.Wrapf( go_errors.New("<this is broken>"), "<it is not working>", )}, - //and when we wrap an error that already has a stack, it will not duplicate, - //note that we always want an explanation which is just to enforce good practice: + // and when we wrap an error that already has a stack, it will not duplicate, + // note that we always want an explanation which is just to enforce good practice: {title: "uafrica.Wrapf(pkg.New(...))", err: uf_errors.Wrapf( pkg_errors.New("<this is broken>"), "<it is not working>", )}, - //pkg can also wrap an error with a text explanation, - //but the synctax is annoying as one has to add the stack and the message separately - //(compared to a simple Wrapf() call to imply a stack as well...) + // pkg can also wrap an error with a text explanation, + // but the synctax is annoying as one has to add the stack and the message separately + // (compared to a simple Wrapf() call to imply a stack as well...) - //we firstly wrap an error that has NO stack: + // we firstly wrap an error that has NO stack: {title: "pkg.WithMessage(pkg.WithStack(go.New()))", err: pkg_errors.WithMessage( pkg_errors.WithStack( go_errors.New("<this is broken>"), @@ -78,11 +78,11 @@ func init() { "<it is not working>", )}, - //we will typically pass an error up several layers before it is reported - //and the original error may come without a stack, - //and another package might have added a stack using pkg - //then we still add our own explanations on top of that - //in both the following two examples, we have the following stack: + // we will typically pass an error up several layers before it is reported + // and the original error may come without a stack, + // and another package might have added a stack using pkg + // then we still add our own explanations on top of that + // in both the following two examples, we have the following stack: // // db not open (without a stack) // query failed (without a stack, using fmt.Errorf("query failed: %v", err)) @@ -90,10 +90,10 @@ func init() { // failed to read users from db (demonstrated with pkg.WithMessage() and our own Wrapf()) // cannot display list of users (demonstrated with pkg.WithMessage() and our own Wrapf()) // - //first using only pkg: + // first using only pkg: {title: "full stack with pkg", err: pkg_errors.WithMessage( pkg_errors.WithMessage( - pkg_errors.WithStack( //assuming this comes from 3rd party lib e.g. sql or pg - we cannot change this or deeper errors + pkg_errors.WithStack( // assuming this comes from 3rd party lib e.g. sql or pg - we cannot change this or deeper errors pkg_errors.WithMessage( fmt.Errorf( "query failed: %v", @@ -107,10 +107,10 @@ func init() { "cannot display list of users", )}, - //same thing but wrapping at the top with out own messages and stack + // same thing but wrapping at the top with out own messages and stack {title: "full stack with uafrica", err: uf_errors.Wrapf( uf_errors.Wrapf( - pkg_errors.WithStack( //assuming this comes from 3rd party lib e.g. sql or pg - we cannot change this or deeper errors + pkg_errors.WithStack( // assuming this comes from 3rd party lib e.g. sql or pg - we cannot change this or deeper errors pkg_errors.WithMessage( fmt.Errorf( "query failed: %v", @@ -124,9 +124,9 @@ func init() { "cannot display list of users", )}, - //============================================================================================================================================ - //in the final full stack case, here is the comparison: - //============================================================================================================================================ + // ============================================================================================================================================ + // in the final full stack case, here is the comparison: + // ============================================================================================================================================ // OUTPUT OF err.Errorf() // ---------------------- // @@ -135,7 +135,7 @@ func init() { // uafrica: cannot display list of users, because failed to read users from db, because failed to read list: query failed: db not open // // (at the end, uafrica could not insert ", because ..." because the deepest level was just text from fmt.Errorf(...)) - //============================================================================================================================================ + // ============================================================================================================================================ // OUTPUT OF Printf("%s") for simple error // --------------------------------------- // @@ -144,7 +144,7 @@ func init() { // uafrica: cannot display list of users // // Note that pkg has no simple way to just display the top level error, while we can do it with formatting directive "%s" - //============================================================================================================================================ + // ============================================================================================================================================ // OUTPUT OF Printf("%+v") for full error stack // -------------------------------------------- // @@ -178,7 +178,7 @@ func init() { // uafrica mention each line that handled the error: 111, 112, 113 (called libs in line 114..117 did not include the stack) // - pkg reports full package and FULL filename (host path where the code was compiled) which is not useful in production // uafrica reports the package and BASE filename and function name instead. - //============================================================================================================================================ + // ============================================================================================================================================ // Compact Stack: // -------------- // In streaming log files we do not want a multi-line display of error messages, so uafrica also supports "%c" and "%+c" formatting to print @@ -204,7 +204,7 @@ func init() { // command-line-arguments_test.init/error_formats_test.go(112): 0() failed to read users from db, because // command-line-arguments_test.init.0/error_formats_test.go(113): init.0(): failed to read list: query failed: db not open // - //============================================================================================================================================ + // ============================================================================================================================================ // JSON: // ----- // Having our own errors package allows us also flexibility to do things like creating a JSON error description. @@ -249,7 +249,7 @@ func init() { // } // } // } - //============================================================================================================================================ + // ============================================================================================================================================ } } @@ -265,13 +265,13 @@ func show(t *testing.T, title string, err error) { t.Logf("------------------------------------------------------------------------------------") t.Logf("err.Error(): %s", err.Error()) t.Logf("Printf(%%s): %s", err) - if _, ok := err.(*uf_errors.CustomError); ok { //compact only supported on our own lib: + if _, ok := err.(*uf_errors.CustomError); ok { // compact only supported on our own lib: t.Logf("Printf(%%c): %c", err) t.Logf("Printf(%%+c): %+c", err) } t.Logf("Printf(%%v): %v", err) t.Logf("Printf(%%+v): %+v", err) - if e, ok := err.(*uf_errors.CustomError); ok { //description only supported on our own lib: + if e, ok := err.(*uf_errors.CustomError); ok { // description only supported on our own lib: desc := e.Description() jsonDesc, _ := json.Marshal(desc) t.Logf("JSON: %s", string(jsonDesc)) diff --git a/errors/errors_test.go b/errors/errors_test.go index 75bf7a3e85ee6666663530fa8f31d6b7d2a94a51..a94762daffc6be566e28594a5b8fc99fc4f9224a 100644 --- a/errors/errors_test.go +++ b/errors/errors_test.go @@ -5,40 +5,40 @@ import ( "fmt" "testing" - "gitlab.com/uafrica/go-utils/errors" + "gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/errors" ) func TestErrorFormatting(t *testing.T) { - //pretend this is your error stack - //the original error is created by 3rd party library e.g. pg or mysql and may not have stack info - //but from that point on, we use our errors package which includes stack info... + // pretend this is your error stack + // the original error is created by 3rd party library e.g. pg or mysql and may not have stack info + // but from that point on, we use our errors package which includes stack info... e1 := errors.Errorf("you have problem in your SQL near xxx") e2 := errors.Wrapf(e1, "query failed") e3 := errors.Wrapf(e2, "failed to find account") e4 := errors.Wrapf(e3, "login failed").(*errors.CustomError) - //we can log the error in different ways: + // we can log the error in different ways: - //use %s for a simple message - //-> "login failed" + // use %s for a simple message + // -> "login failed" t.Logf("%%s: %s", e4) - //use either Error() or "%v" for concatenated message: - //-> "login failed, because failed to find account, because query failed, because you have problem in your SQL near xxx" + // use either Error() or "%v" for concatenated message: + // -> "login failed, because failed to find account, because query failed, because you have problem in your SQL near xxx" t.Logf("Error(): %s", e4.Error()) t.Logf("%%v: %v", e4) - //use %+v for complete error with stack: - //-> "gitlab.com/uafrica/go-utils/errors_test/TestErrorFormatting():errors_test.go(17): login failed, because gitlab.com/uafrica/go-utils/errors_test/TestErrorFormatting():errors_test.go(16): failed to find account, because gitlab.com/uafrica/go-utils/errors_test/TestErrorFormatting():errors_test.go(15): query failed, because you have problem in your SQL near xxx + // use %+v for complete error with stack: + // -> "gitlab.com/uafrica/go-utils/errors_test/TestErrorFormatting():errors_test.go(17): login failed, because gitlab.com/uafrica/go-utils/errors_test/TestErrorFormatting():errors_test.go(16): failed to find account, because gitlab.com/uafrica/go-utils/errors_test/TestErrorFormatting():errors_test.go(15): query failed, because you have problem in your SQL near xxx t.Logf("%%+v: %+v", e4) - //you may also JSON marshal the error - //-> + // you may also JSON marshal the error + // -> jsonError, _ := json.Marshal(e4.Description()) t.Logf("json: %s", string(jsonError)) - //using the Description(), one can also verify that the correct - //source references are reported + // using the Description(), one can also verify that the correct + // source references are reported desc := e4.Description() if desc.Source == nil || desc.Source.Line != 18 { t.Fatalf("failed not on line 18") @@ -54,7 +54,7 @@ func TestErrorFormatting(t *testing.T) { } func TestErrorStack(t *testing.T) { - //if some function (e.g. 3rd party) fail without stack, then store the calling stack and append as it is wrapped from there... + // if some function (e.g. 3rd party) fail without stack, then store the calling stack and append as it is wrapped from there... err := a(1) t.Logf("err: %+v", err) } @@ -93,7 +93,7 @@ func f(i int) error { } func TestIs(t *testing.T) { - //in some condition program returns + // in some condition program returns for n := 0; n <= 4; n++ { if err := ReadDb(n); err != nil { @@ -110,7 +110,7 @@ func TestIs(t *testing.T) { } } - //same but err is wrapped deeper + // same but err is wrapped deeper for n := 0; n <= 4; n++ { if err := UserExists(n); err != nil { switch { diff --git a/errors/http_error_test.go b/errors/http_error_test.go index 97d7ae35502c545263db621113a6d168df81a3eb..e5cc400cab12786773e2591a6eccb9028d015e12 100644 --- a/errors/http_error_test.go +++ b/errors/http_error_test.go @@ -4,42 +4,42 @@ import ( "net/http" "testing" - "gitlab.com/uafrica/go-utils/errors" + "gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/errors" ) func TestHTTPError(t *testing.T) { var err error - //you can wrap any error with an HTTP code: + // you can wrap any error with an HTTP code: err = errors.Errorf("failed to connect to db") - //err = errors.HTTP(http.StatusInternalServerError, err) + // err = errors.HTTP(http.StatusInternalServerError, err) - //or if you know you are creating the HTTP error, do it as one statement + // or if you know you are creating the HTTP error, do it as one statement err = errors.HTTP(http.StatusBadRequest, err, "failed to get user") - //and higher again -... many layers... + // and higher again -... many layers... err = errors.HTTP(http.StatusNotFound, err, "terrible mistake") - //now log: + // now log: t.Logf("failed:\n\t%+v", err) - t.Logf("HTTP Code: %d", errors.HTTPCode(err)) //will return smallest code in the stack, 0 if none. + t.Logf("HTTP Code: %d", errors.HTTPCode(err)) // will return smallest code in the stack, 0 if none. - //you can wrap any error with an HTTP code: + // you can wrap any error with an HTTP code: err = errors.Errorf("failed to connect to db") - //err = errors.HTTP(http.StatusInternalServerError, err) + // err = errors.HTTP(http.StatusInternalServerError, err) - //and higher again -... many layers... + // and higher again -... many layers... err = errors.HTTP(http.StatusNotFound, err, "terrible mistake") - //or if you know you are creating the HTTP error, do it as one statement + // or if you know you are creating the HTTP error, do it as one statement err = errors.HTTP(http.StatusBadRequest, err, "failed to get user") - //now log: + // now log: t.Logf("failed:\n\t%+v", err) - t.Logf("HTTP Code: %d", errors.HTTPCode(err)) //will return smallest code in the stack, 0 if none. + t.Logf("HTTP Code: %d", errors.HTTPCode(err)) // will return smallest code in the stack, 0 if none. err = errors.Errorf("failed to connect to db") err = errors.Wrapf(err, "failed to connect to db") t.Logf("failed:\n\t%+v", err) - t.Logf("HTTP Code: %d", errors.HTTPCode(err)) //will return smallest code in the stack, 0 if none. + t.Logf("HTTP Code: %d", errors.HTTPCode(err)) // will return smallest code in the stack, 0 if none. } diff --git a/go.mod b/go.mod index 55391096b8225c1b3def8ea8c8d90b012d098ac0..e92f081dd5b146634f9c64db1d632e421db932d1 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ -module gitlab.com/uafrica/go-utils +module gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils -go 1.17 +go 1.18 require ( github.com/MindscapeHQ/raygun4go v1.1.1 diff --git a/handler_utils/api.go b/handler_utils/api.go index 4778a75dd85a15e54f2f322f01a494a04e2105a6..0c14596501b845e90a2facd030f94ac596b7e2ac 100644 --- a/handler_utils/api.go +++ b/handler_utils/api.go @@ -2,7 +2,7 @@ package handler_utils import ( "github.com/aws/aws-lambda-go/events" - "gitlab.com/uafrica/go-utils/errors" + "gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/errors" "reflect" "strconv" "strings" @@ -36,7 +36,7 @@ func ValidateAPIEndpoints(endpoints map[string]map[string]interface{}) (map[stri if err != nil { return nil, errors.Wrapf(err, "%s %s has invalid handler %T", method, resource, handlerFunc) } - //replace the endpoint value so that we can quickly call this handler + // replace the endpoint value so that we can quickly call this handler endpoints[resource][method] = handler } } @@ -59,20 +59,20 @@ func ValidateRequestParams(request *events.APIGatewayProxyRequest, paramsStructT continue } - //get value(s) from query string + // get value(s) from query string var paramStrValues []string if paramStrValue, isDefined := request.QueryStringParameters[n]; isDefined { - paramStrValues = []string{paramStrValue} //single value + paramStrValues = []string{paramStrValue} // single value } else { paramStrValues = request.MultiValueQueryStringParameters[n] } if len(paramStrValues) == 0 { - continue //param has no value specified in URL + continue // param has no value specified in URL } - //param is defined >=1 times in URL + // param is defined >=1 times in URL if f.Type.Kind() == reflect.Slice { - //iterate over all specified values + // iterate over all specified values for _, paramStrValue := range paramStrValues { newValuePtr := reflect.New(f.Type.Elem()) if err := setParamFromStr( @@ -86,12 +86,12 @@ func ValidateRequestParams(request *events.APIGatewayProxyRequest, paramsStructT if len(paramStrValues) > 1 { return reflect.Value{}, errors.Errorf("%s does not support >1 values(%v)", n, strings.Join(paramStrValues, ",")) } - //single value specified + // single value specified if err := setParamFromStr(paramsStructValuePtr.Elem().Field(i), paramStrValues[0]); err != nil { return reflect.Value{}, errors.Wrapf(err, "failed to set %s=%s", n, paramStrValues[0]) } } - } //for each param struct field + } // for each param struct field return paramsStructValuePtr, nil } @@ -99,7 +99,7 @@ func ValidateRequestParams(request *events.APIGatewayProxyRequest, paramsStructT func setParamFromStr(fieldValue reflect.Value, paramStrValue string) error { switch fieldValue.Type().Kind() { case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - //parse to int for this struct field + // parse to int for this struct field if i64, err := strconv.ParseInt(paramStrValue, 10, 64); err != nil { return errors.Errorf("%s is not a number", paramStrValue) } else { @@ -118,7 +118,7 @@ func setParamFromStr(fieldValue reflect.Value, paramStrValue string) error { } case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: - //parse to int for this struct field + // parse to int for this struct field if u64, err := strconv.ParseUint(paramStrValue, 10, 64); err != nil { return errors.Errorf("%s is not a number", paramStrValue) } else { @@ -161,6 +161,6 @@ func setParamFromStr(fieldValue reflect.Value, paramStrValue string) error { default: return errors.Errorf("unsupported type %v", fieldValue.Type().Kind()) - } //switch param struct field + } // switch param struct field return nil } diff --git a/handler_utils/cron.go b/handler_utils/cron.go index 8fbd9b3aa1dd929fbfc3f17ee974a0df1739f879..78fcc08bbd894b660b5b75a07e4439205b6cf960 100644 --- a/handler_utils/cron.go +++ b/handler_utils/cron.go @@ -1,8 +1,8 @@ package handler_utils import ( - "gitlab.com/uafrica/go-utils/errors" - "gitlab.com/uafrica/go-utils/logs" + "gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/errors" + "gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/logs" ) // ValidateCronHandlers checks that all handlers are correctly defined using one of the supported handler types diff --git a/handler_utils/handler.go b/handler_utils/handler.go index b1e6cad0926cd5941ec66f73b9462fdbfed6eed0..56a9af09ca3eb7b6abc1ea666c5297a0bf9a8dc0 100644 --- a/handler_utils/handler.go +++ b/handler_utils/handler.go @@ -3,7 +3,7 @@ package handler_utils import ( "reflect" - "gitlab.com/uafrica/go-utils/errors" + "gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/errors" ) type Handler struct { @@ -29,15 +29,15 @@ func NewHandler(handlerFunction interface{}) (Handler, error) { return h, errors.Errorf("returns %d results instead of ([Response,] error)", handlerFunctionType.NumOut()) } - //arg[0] must be a struct for params. It may be an empty struct, but - //all public fields require a json tag which we will use to math the URL param name + // arg[0] must be a struct for params. It may be an empty struct, but + // all public fields require a json tag which we will use to math the URL param name if err := validateStructType(handlerFunctionType.In(0)); err != nil { return h, errors.Wrapf(err, "first arg %v is not valid params struct type", handlerFunctionType.In(0)) } h.RequestParamsType = handlerFunctionType.In(0) - //arg[1] is optional and must be a struct for request body. It may be an empty struct, but - //all public fields require a json tag which we will use to unmarshal the request body from JSON + // arg[1] is optional and must be a struct for request body. It may be an empty struct, but + // all public fields require a json tag which we will use to unmarshal the request body from JSON if handlerFunctionType.NumIn() >= 2 { if handlerFunctionType.In(1).Kind() == reflect.Slice { if err := validateStructType(handlerFunctionType.In(1).Elem()); err != nil { @@ -49,22 +49,22 @@ func NewHandler(handlerFunction interface{}) (Handler, error) { } } - //todo: check special fields for claims, and see if also applies to params struct... - //AccountID must be int64 or *int64 with tag =??? - //UserID must be int64 or *int64 with tag =??? - //Username must be string with tag =??? + // todo: check special fields for claims, and see if also applies to params struct... + // AccountID must be int64 or *int64 with tag =??? + // UserID must be int64 or *int64 with tag =??? + // Username must be string with tag =??? h.RequestBodyType = handlerFunctionType.In(1) } - //last result must be error + // last result must be error if _, ok := reflect.New(handlerFunctionType.Out(handlerFunctionType.NumOut() - 1)).Interface().(*error); !ok { return h, errors.Errorf("last result %v is not error type", handlerFunctionType.Out(handlerFunctionType.NumOut()-1)) } // First result is the response object if handlerFunctionType.NumOut() > 0 { - h.ResponseType = handlerFunctionType.Out(0) + h.ResponseType = handlerFunctionType.Out(0) } h.FuncValue = reflect.ValueOf(handlerFunction) @@ -78,7 +78,7 @@ func validateStructType(t reflect.Type) error { for i := 0; i < t.NumField(); i++ { f := t.Field(i) if f.Name[0] >= 'a' && f.Name[0] <= 'z' { - //lowercase fields should not have json tag + // lowercase fields should not have json tag if f.Tag.Get("json") != "" { return errors.Errorf("%s.%s must be uppercase because it has a json tag \"%s\"", t.Name(), diff --git a/handler_utils/sqs.go b/handler_utils/sqs.go index 4fd0727e286973fa6498cae28ce49a7de16d4494..1c25266134a5e753bfd60d25bf9c17dae17865a0 100644 --- a/handler_utils/sqs.go +++ b/handler_utils/sqs.go @@ -3,10 +3,10 @@ package handler_utils import ( "encoding/json" "github.com/aws/aws-lambda-go/events" - "gitlab.com/uafrica/go-utils/errors" - "gitlab.com/uafrica/go-utils/logs" - "gitlab.com/uafrica/go-utils/s3" - "gitlab.com/uafrica/go-utils/sqs" + "gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/errors" + "gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/logs" + "gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/s3" + "gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/sqs" "reflect" ) diff --git a/logs/logs_test.go b/logs/logs_test.go index 78fb518933463d6f0f5b8a10ad9792b26f11aabb..2eb9ff7620a486d739fea834e221a04e0bc8044c 100644 --- a/logs/logs_test.go +++ b/logs/logs_test.go @@ -4,15 +4,15 @@ import ( "os" "testing" - "gitlab.com/uafrica/go-utils/errors" - "gitlab.com/uafrica/go-utils/logs" + "gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/errors" + "gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/logs" ) func TestLogs(t *testing.T) { - //requestID := t.Name() - //event := events.APIGatewayProxyRequest{} + // requestID := t.Name() + // event := events.APIGatewayProxyRequest{} os.Setenv("DEBUGGING", "true") - //logs.InitLogs(&requestID, &event) + // logs.InitLogs(&requestID, &event) // formatter := log.TextFormatter{} // log.SetFormatter(&formatter) @@ -24,9 +24,9 @@ func TestLogs(t *testing.T) { logs.Warn("Warning about a=%s,%s,%s", "a", "b", "c") logs.WarnWithFields(map[string]interface{}{"a": 4, "b": 5}, errors.Errorf("Cant believe it failed")) logs.SQLDebugInfo("SELECT * from user") - //logs.LogRequestInfo(event) - //api_logs.LogSQSEvent(sqsEvent) + // logs.LogRequestInfo(event) + // api_logs.LogSQSEvent(sqsEvent) - //api_logs.Errorf("Debugging %d!", 456) - //api_logs.Error("Info") + // api_logs.Errorf("Debugging %d!", 456) + // api_logs.Error("Info") } diff --git a/logs/sensitive_words_test.go b/logs/sensitive_words_test.go index 53a51d09b657ae2b853d97c38a34aaead144192c..eb8e5585f3e488bc942c634aa804f28081dab0ae 100644 --- a/logs/sensitive_words_test.go +++ b/logs/sensitive_words_test.go @@ -3,35 +3,35 @@ package logs_test import ( "testing" - "gitlab.com/uafrica/go-utils/logs" + "gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/logs" ) func TestFilter(t *testing.T) { tests := []test{ - {"Your new OTP is 12345", map[string]bool{"12345": true}, "Your new OTP is ***"}, //match at end of string - {"Your new OTP is 12345.", map[string]bool{"12345": true}, "Your new OTP is ***."}, //match in middle - {"12345 is your new OTP.", map[string]bool{"12345": true}, "*** is your new OTP."}, //match at start - {"Your new OTP is 12345. Do not repeat 12345/123456", map[string]bool{"12345": true}, "Your new OTP is ***. Do not repeat ***/123456"}, //no match on longer word - {"Send 12345\n12345 is the code you should type", map[string]bool{"12345": true}, "Send ***\n*** is the code you should type"}, //match before/after newline + {"Your new OTP is 12345", map[string]bool{"12345": true}, "Your new OTP is ***"}, // match at end of string + {"Your new OTP is 12345.", map[string]bool{"12345": true}, "Your new OTP is ***."}, // match in middle + {"12345 is your new OTP.", map[string]bool{"12345": true}, "*** is your new OTP."}, // match at start + {"Your new OTP is 12345. Do not repeat 12345/123456", map[string]bool{"12345": true}, "Your new OTP is ***. Do not repeat ***/123456"}, // no match on longer word + {"Send 12345\n12345 is the code you should type", map[string]bool{"12345": true}, "Send ***\n*** is the code you should type"}, // match before/after newline // {"Your new OTP is 111-11", map[string]bool{"111-11": true}, "Your new OTP is ***"}, - {"The secret IP is 1.2.3.4 but 11.2.3.4 and 1.2.3.44 is public", map[string]bool{"1.2.3.4": true}, "The secret IP is *** but 11.2.3.4 and 1.2.3.44 is public"}, //match with delimiters in the word + {"The secret IP is 1.2.3.4 but 11.2.3.4 and 1.2.3.44 is public", map[string]bool{"1.2.3.4": true}, "The secret IP is *** but 11.2.3.4 and 1.2.3.44 is public"}, // match with delimiters in the word // - {"1.2.3.4.5.6.7.8", map[string]bool{"1": true}, "***.2.3.4.5.6.7.8"}, //short replace at start - {"1.2.3.4.5.6.7.8", map[string]bool{"4": true}, "1.2.3.***.5.6.7.8"}, //short replace in middle - {"1.2.3.4.5.6.7.8", map[string]bool{"8": true}, "1.2.3.4.5.6.7.***"}, //short replace at end + {"1.2.3.4.5.6.7.8", map[string]bool{"1": true}, "***.2.3.4.5.6.7.8"}, // short replace at start + {"1.2.3.4.5.6.7.8", map[string]bool{"4": true}, "1.2.3.***.5.6.7.8"}, // short replace in middle + {"1.2.3.4.5.6.7.8", map[string]bool{"8": true}, "1.2.3.4.5.6.7.***"}, // short replace at end // - {"11.22.33.44.55.66.77.88", map[string]bool{"11": true}, "***.22.33.44.55.66.77.88"}, //short replace at start - {"11.22.33.44.55.66.77.88", map[string]bool{"44": true}, "11.22.33.***.55.66.77.88"}, //short replace in middle - {"11.22.33.44.55.66.77.88", map[string]bool{"88": true}, "11.22.33.44.55.66.77.***"}, //short replace at end + {"11.22.33.44.55.66.77.88", map[string]bool{"11": true}, "***.22.33.44.55.66.77.88"}, // short replace at start + {"11.22.33.44.55.66.77.88", map[string]bool{"44": true}, "11.22.33.***.55.66.77.88"}, // short replace in middle + {"11.22.33.44.55.66.77.88", map[string]bool{"88": true}, "11.22.33.44.55.66.77.***"}, // short replace at end // - {"111.222.333.444.555.666.777.888", map[string]bool{"111": true}, "***.222.333.444.555.666.777.888"}, //exact replace at start - {"111.222.333.444.555.666.777.888", map[string]bool{"444": true}, "111.222.333.***.555.666.777.888"}, //exact replace in middle - {"111.222.333.444.555.666.777.888", map[string]bool{"888": true}, "111.222.333.444.555.666.777.***"}, //exact replace at end + {"111.222.333.444.555.666.777.888", map[string]bool{"111": true}, "***.222.333.444.555.666.777.888"}, // exact replace at start + {"111.222.333.444.555.666.777.888", map[string]bool{"444": true}, "111.222.333.***.555.666.777.888"}, // exact replace in middle + {"111.222.333.444.555.666.777.888", map[string]bool{"888": true}, "111.222.333.444.555.666.777.***"}, // exact replace at end // - {"1111.2222.3333.4444.5555.6666.7777.8888", map[string]bool{"1111": true}, "***.2222.3333.4444.5555.6666.7777.8888"}, //long replace at start - {"1111.2222.3333.4444.5555.6666.7777.8888", map[string]bool{"4444": true}, "1111.2222.3333.***.5555.6666.7777.8888"}, //long replace in middle - {"1111.2222.3333.4444.5555.6666.7777.8888", map[string]bool{"8888": true}, "1111.2222.3333.4444.5555.6666.7777.***"}, //long replace at end + {"1111.2222.3333.4444.5555.6666.7777.8888", map[string]bool{"1111": true}, "***.2222.3333.4444.5555.6666.7777.8888"}, // long replace at start + {"1111.2222.3333.4444.5555.6666.7777.8888", map[string]bool{"4444": true}, "1111.2222.3333.***.5555.6666.7777.8888"}, // long replace in middle + {"1111.2222.3333.4444.5555.6666.7777.8888", map[string]bool{"8888": true}, "1111.2222.3333.4444.5555.6666.7777.***"}, // long replace at end } for testNr, test := range tests { filtered, changed := logs.FilterSensitiveWordsMap(test.Text, test.Words) diff --git a/logs/stack.go b/logs/stack.go index 05cfc092a89f16221f60f9fe145efa9295c8c0bf..ec66655ef36c60d50f354dcb514997235c435eb1 100644 --- a/logs/stack.go +++ b/logs/stack.go @@ -6,11 +6,11 @@ import ( "strconv" "strings" - "gitlab.com/uafrica/go-utils/errors" + "gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/errors" ) type Stack struct { - Routine int64 //go routine nr that crashed... + Routine int64 // go routine nr that crashed... Callers []errors.CallerInfo } @@ -19,12 +19,12 @@ func CallStack() Stack { Callers: []errors.CallerInfo{}, } - //get the call stack + // get the call stack s := bufio.NewScanner(strings.NewReader(string(debug.Stack()))) - //expect stack to look like this: + // expect stack to look like this: // "goroutine 14 [running]:\nruntime/debug.Stack()\n\t/usr/local/go/src/runtime/debug/stack.go:24 +0x88\ngitlab.com/uafrica/go-utils/api.Api.Handler.func3.1(0x1400009ff60)\n\t/Users/jansemmelink/uafrica/go-utils/api/lambda.go:210 +0x48\npanic({0x100780d20, 0x100b98a50})\n\t/usr/local/go/src/runtime/panic.go:1038 +0x21c\ngitlab.com/uafrica/go-utils/examples/core/api/users.Crash({{{0x1008237e0, 0x1400040af30}, {0x4, {0x1008186e0, 0x14000010020}, 0x1400040af90, 0x25, 0x2f}, {0xc04f2cb513fbdc28, 0x103ccb5b4c, ...}}, ...}, ...)\n\t/Users/jansemmelink/uafrica/go-utils/examples/core/api/users/users.go:115 +0x20\nreflect.Value.call({0x10076bba0, 0x10080cdc8, 0x13}, {0x10059bb1c, 0x4}, {0x140000a0730, 0x2, 0x2})\n\t/usr/local/go/src/reflect/value.go:543 +0x584\nreflect.Value.Call({0x10076bba0, 0x10080cdc8, 0x13}, {0x140000a0730, 0x2, 0x2})\n\t/usr/local/go/src/reflect/value.go:339 +0x8c\ngitlab.com/uafrica/go-utils/api.Api.Handler.func3({{0x10082f730, 0x100780960}, {0x0, 0x0}, {0x0, 0x0}, {0x10076bba0, 0x10080cdc8, 0x13}}, {0x140000a0730, ...})\n\t/Users/jansemmelink/uafrica/go-utils/api/lambda.go:214 +0x84\ngitlab.com/uafrica/go-utils/api.Api.Handler({{0x10082b3f0, 0x140003b6480}, {0x10059b80a, 0x3}, {0x140003b6360}, {0x1005a3606, 0x12}, 0x10080cf98, {0x1008190e0, 0x100bdd308}, ...}, ...)\n\t/Users/jansemmelink/uafrica/go-utils/api/lambda.go:216 +0x1238\ngitlab.com/uafrica/go-utils/api.Api.ServeHTTP({{0x10082b3f0, 0x140003b6480}, {0x10059b80a, 0x3}, {0x140003b6360}, {0x1005a3606, 0x12}, 0x10080cf98, {0x1008190e0, 0x100bdd308}, ...}, ...)\n\t/Users/jansemmelink/uafrica/go-utils/api/local.go:81 +0x6ac\nnet/http.serverHandler.ServeHTTP({0x1400015a620}, {0x1008200e8, 0x1400015aa80}, 0x140003c6900)\n\t/usr/local/go/src/net/http/server.go:2878 +0x444\nnet/http.(*conn).serve(0x14000279220, {0x1008237e0, 0x140003b6780})\n\t/usr/local/go/src/net/http/server.go:1929 +0xb6c\ncreated by net/http.(*Server).Serve\n\t/usr/local/go/src/net/http/server.go:3033 +0x4b8\n" - //i.e. multiple lines ending with "\n" each, e.g.: - //------------------------------------------------------------------------------------------------------------ + // i.e. multiple lines ending with "\n" each, e.g.: + // ------------------------------------------------------------------------------------------------------------ // goroutine 37 [running]: // runtime/debug.Stack() // /usr/local/go/src/runtime/debug/stack.go:24 +0x88 @@ -35,9 +35,9 @@ func CallStack() Stack { // gitlab.com/uafrica/go-utils/examples/core/api/users.Crash({{{0x100cab7c0, 0x140004843c0}, {0x4, {0x100ca06c0, 0x14000138010}, 0x14000484420, 0x20, 0x28}, {0xc04f2d6c3023b330, 0x227861e6a, ...}}, ...}, ...) // /Users/jansemmelink/uafrica/go-utils/examples/core/api/users/users.go:115 +0x20 // ... - //------------------------------------------------------------------------------------------------------------ + // ------------------------------------------------------------------------------------------------------------ - //get go routine nr from first line: "gorouting <nr> [running]:" + // get go routine nr from first line: "gorouting <nr> [running]:" if s.Scan() { p := strings.SplitN(s.Text(), " ", 3) if len(p) >= 2 && p[0] == "goroutine" { @@ -48,9 +48,9 @@ func CallStack() Stack { } } - //next expect line pairs for each level of the stack + // next expect line pairs for each level of the stack for { - //read first line in this pair, expecting <package>.<funcName>(<args>) + // read first line in this pair, expecting <package>.<funcName>(<args>) if !s.Scan() { break } @@ -61,18 +61,18 @@ func CallStack() Stack { } line2 := s.Text() - //fmt.Printf(" STACK LINE: %s %s\n", line1, line2) + // fmt.Printf(" STACK LINE: %s %s\n", line1, line2) - //split line 1 on any bracket or comma to get "<package>.<funcName>"["<arg>" ...] - //func may have multiple '.', so do not split on that yet! + // split line 1 on any bracket or comma to get "<package>.<funcName>"["<arg>" ...] + // func may have multiple '.', so do not split on that yet! line1Fields := strings.FieldsFunc(line1, func(c rune) bool { return strings.Contains("(), ", string(c)) }) - //split line 2 <file>:<line> +0x##... + // split line 2 <file>:<line> +0x##... line2Fields := strings.FieldsFunc(line2, func(c rune) bool { return strings.Contains(": ", string(c)) }) lineNr, _ := strconv.ParseInt(line2Fields[1], 10, 64) caller := errors.NewCaller(line1Fields[0], line2Fields[0], int(lineNr)) - //skip first levels that refer to capturing the stack + // skip first levels that refer to capturing the stack ci := caller.Info() if len(stack.Callers) == 0 { if ci.Package == "runtime/debug" || @@ -82,7 +82,7 @@ func CallStack() Stack { continue } if _, err := strconv.ParseInt(ci.Function, 10, 64); err == nil { - continue //typical defer function without a name used to catch the crash + continue // typical defer function without a name used to catch the crash } } stack.Callers = append(stack.Callers, caller.Info()) diff --git a/redis/redis.go b/redis/redis.go index 1f8545ee421b870cd4ff21998be63e307cd31380..410ab5403502b14cb7b9097e3cd429dc5ef3fa0c 100644 --- a/redis/redis.go +++ b/redis/redis.go @@ -8,8 +8,8 @@ import ( "strings" "time" - "gitlab.com/uafrica/go-utils/errors" - "gitlab.com/uafrica/go-utils/logs" + "gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/errors" + "gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/logs" "github.com/go-redis/redis_rate/v9" @@ -27,7 +27,7 @@ func NewClient(addr string) *ClientWithHelpers { return &ClientWithHelpers{ Client: redis.NewClient(&redis.Options{ MaxRetries: 1, - DialTimeout: time.Duration(1) * time.Second, //So max 2 second wait + DialTimeout: time.Duration(1) * time.Second, // So max 2 second wait Addr: addr, Password: "", // no password set DB: 0, // use default Db diff --git a/reflection/get.go b/reflection/get.go index 956d4c2d5f7c0e3abc792015ca84a4a5677d6144..be155b28904a72afe82cedc477fd6dcc886869e9 100644 --- a/reflection/get.go +++ b/reflection/get.go @@ -5,18 +5,18 @@ import ( "reflect" "strings" - "gitlab.com/uafrica/go-utils/errors" + "gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/errors" ) -//Get() a jq-named element from a value -//e.g. get(reflect.ValueOf(myDoc), ".hits.hits[]._source") -//the result is an array of _source items which may be -//just one field inside the hits object in the list +// Get() a jq-named element from a value +// e.g. get(reflect.ValueOf(myDoc), ".hits.hits[]._source") +// the result is an array of _source items which may be +// just one field inside the hits object in the list // -//see usage in search.TimeSeries.Search() to get documents -//from the OpenSearch response structure that is very nested -//and parse using a runtime-created reflect Type, i.e. one -//cannot get it simply by iterating over res.Hits.[]Hits... +// see usage in search.TimeSeries.Search() to get documents +// from the OpenSearch response structure that is very nested +// and parse using a runtime-created reflect Type, i.e. one +// cannot get it simply by iterating over res.Hits.[]Hits... func Get(v reflect.Value, key string) (reflect.Value, error) { return get("", v, key) } @@ -58,7 +58,7 @@ func get(name string, v reflect.Value, key string) (reflect.Value, error) { return v, errors.Errorf("canot get %s from slice, expecting \"[]\" in the key", key) } - //make array of results from each item in the slice + // make array of results from each item in the slice var result reflect.Value for i := 0; i < v.Len(); i++ { if vv, err := get(fmt.Sprintf("%s[%d]", name, i), v.Index(i), key[2:]); err != nil { diff --git a/reflection/reflection.go b/reflection/reflection.go index 3f3458d34698d87622d7e1c6ba0450480a9c1abd..57a5cbd855ae6c5095769661819c3dcaa137f669 100644 --- a/reflection/reflection.go +++ b/reflection/reflection.go @@ -4,7 +4,7 @@ import ( "reflect" "time" - "gitlab.com/uafrica/go-utils/logs" + "gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/logs" ) func SetPointerTime(field reflect.Value, value *time.Time) { diff --git a/reflection/set.go b/reflection/set.go index d9a323145a5f324260d076ca4c83007ff42a425d..c1424b3c88c1a7b70c957dd6324ca7793ba08808 100644 --- a/reflection/set.go +++ b/reflection/set.go @@ -6,7 +6,7 @@ import ( "strconv" "strings" - "gitlab.com/uafrica/go-utils/errors" + "gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/errors" ) func SetValue(tgt reflect.Value, src interface{}) error { @@ -15,10 +15,10 @@ func SetValue(tgt reflect.Value, src interface{}) error { return nil } - //need some kind of type conversion + // need some kind of type conversion switch tgt.Type().Kind() { case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - //setting a signed integer + // setting a signed integer var i64 int64 strValue := fmt.Sprintf("%v", src) if strValue != "" { @@ -42,7 +42,7 @@ func SetValue(tgt reflect.Value, src interface{}) error { } case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: - //parse to int for this struct field + // parse to int for this struct field var u64 uint64 strValue := fmt.Sprintf("%v", src) if strValue != "" { @@ -95,7 +95,7 @@ func SetValue(tgt reflect.Value, src interface{}) error { default: return errors.Errorf("unsupported type %v", tgt.Type().Kind()) - } //switch param struct field + } // switch param struct field return nil } diff --git a/reflection/type_clone.go b/reflection/type_clone.go index c1427129a73883ce69ae523d8f74052152cb6297..ef2a7359c870a8b30b01ee2fcc605dd242732c73 100644 --- a/reflection/type_clone.go +++ b/reflection/type_clone.go @@ -4,19 +4,19 @@ import ( "reflect" "strings" - "gitlab.com/uafrica/go-utils/errors" + "gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/errors" ) -//CloneType() clones a type into a new type, replacing some elements of it -//Paramters: +// CloneType() clones a type into a new type, replacing some elements of it +// Paramters: // t is the existing type to be cloned // replace is a list of items to replace with their new types // use jq key notation // if this is empty map/nil, it will clone the type without changes -//Return: +// Return: // cloned type or error // -//Example: +// Example: // newType,err := reflection.CloneType( // reflect.TypeOf(myStruct{}), // map[string]reflect.Type{ @@ -25,26 +25,26 @@ import ( // }, // ) // -//This is not a function you will use everyday, but very useful when needed -//See example usage in search to read OpenSearch responses with the correct -//struct type used to parse ".hits.hits[]._source" so that we do not have to -//unmarshal the JSON, then marshal each _source from map[string]interface{} -//back to json then unmarshal again into the correct type! -//It saves a lot of overhead CPU doing it all at once using the correct type -//nested deep into the response body type. +// This is not a function you will use everyday, but very useful when needed +// See example usage in search to read OpenSearch responses with the correct +// struct type used to parse ".hits.hits[]._source" so that we do not have to +// unmarshal the JSON, then marshal each _source from map[string]interface{} +// back to json then unmarshal again into the correct type! +// It saves a lot of overhead CPU doing it all at once using the correct type +// nested deep into the response body type. // -//this function was written for above case... it will likely need extension -//if we want to replace all kinds of other fields, but it meets the current -//requirements. +// this function was written for above case... it will likely need extension +// if we want to replace all kinds of other fields, but it meets the current +// requirements. // -//After parsing, use reflection.Get() with the same key notation to get the -//result from the nested document. +// After parsing, use reflection.Get() with the same key notation to get the +// result from the nested document. // -//Note current shortcoming: partial matching will apply if you have two -//similarly named fields, e.g. "name" and "name2", then replace instruction -//on "name" may partial match name2. To fix this, we need better function -//than strings.HasPrefix() to check for delimiter/end of name to do full -//word matches, and we need to extend the test to illustrate this. +// Note current shortcoming: partial matching will apply if you have two +// similarly named fields, e.g. "name" and "name2", then replace instruction +// on "name" may partial match name2. To fix this, we need better function +// than strings.HasPrefix() to check for delimiter/end of name to do full +// word matches, and we need to extend the test to illustrate this. func CloneType(t reflect.Type, replace map[string]reflect.Type) (reflect.Type, error) { cloned, err := clone("", t, replace) if err != nil { @@ -62,7 +62,7 @@ func clone(name string, t reflect.Type, replace map[string]reflect.Type) (reflec fields := []reflect.StructField{} for i := 0; i < t.NumField(); i++ { f := t.Field(i) - n := strings.SplitN(f.Tag.Get("json"), ",", 2)[0] //exclude ,omitempty... + n := strings.SplitN(f.Tag.Get("json"), ",", 2)[0] // exclude ,omitempty... if n == "" { n = f.Name } @@ -92,11 +92,11 @@ func clone(name string, t reflect.Type, replace map[string]reflect.Type) (reflec for replaceName, replaceType := range replace { if strings.HasPrefix(replaceName, name+"[]") { if replaceName == name+"[]" { - //full match + // full match delete(replace, replaceName) return replaceType, nil } - //partial match + // partial match elemType, err := clone(name+"[]", t.Elem(), replace) if err != nil { return t, errors.Wrapf(err, "failed to clone slice elem type") @@ -104,7 +104,7 @@ func clone(name string, t reflect.Type, replace map[string]reflect.Type) (reflec return reflect.SliceOf(elemType), nil } } - //no match + // no match return t, nil default: diff --git a/reflection/type_clone_test.go b/reflection/type_clone_test.go index 474bc098c500a7aca52106681e6cdcd072af65c0..f655ff3691082704b9628099da914308c5030c87 100644 --- a/reflection/type_clone_test.go +++ b/reflection/type_clone_test.go @@ -5,7 +5,7 @@ import ( "reflect" "testing" - "gitlab.com/uafrica/go-utils/reflection" + "gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/reflection" ) func Test1(t *testing.T) { @@ -51,7 +51,7 @@ func Test1(t *testing.T) { } ` - //using default type, documents in _source:{} are parsed to map[string]interface{} + // using default type, documents in _source:{} are parsed to map[string]interface{} res := SearchResponseBody{} if err := json.Unmarshal([]byte(doc), &res); err != nil { t.Fatalf("cannot unmarshal response into default type: %+v", err) @@ -60,7 +60,7 @@ func Test1(t *testing.T) { t.Logf("doc[%d]: (%T)%+v", hitIndex, hit.Source, hit.Source) } - //create type with own document type to use for _source field: + // create type with own document type to use for _source field: cloned, err := reflection.CloneType( reflect.TypeOf(SearchResponseBody{}), map[string]reflect.Type{ @@ -71,7 +71,7 @@ func Test1(t *testing.T) { } t.Logf("cloned type: %v", cloned) - //unmarshal using new type to have correct type for each hit + // unmarshal using new type to have correct type for each hit resPtrValue := reflect.New(cloned) if err := json.Unmarshal([]byte(doc), resPtrValue.Interface()); err != nil { t.Fatalf("failed to decode into cloned type: %+v", err) @@ -79,7 +79,7 @@ func Test1(t *testing.T) { // clonedRes := resPtrValue.Interface() // t.Logf("Coned res: %+v", clonedRes) - //get the replaced values as an array of docs + // get the replaced values as an array of docs v, err := reflection.Get(resPtrValue, ".hits.hits[]._source") if err != nil { t.Fatalf("Did not get list: %+v", err) @@ -97,7 +97,7 @@ func Test1(t *testing.T) { } type SearchResponseBody struct { - Took int `json:"took"` //milliseconds + Took int `json:"took"` // milliseconds TimedOut bool `json:"timed_out"` Shards SearchResponseShards `json:"_shards"` Hits SearchResponseHits `json:"hits"` @@ -117,16 +117,16 @@ type SearchResponseHits struct { } type SearchResponseHitsTotal struct { - Value int `json:"value"` //e.g. 0 when no docs matched - Relation string `json:"relation"` //e.g. "eq" + Value int `json:"value"` // e.g. 0 when no docs matched + Relation string `json:"relation"` // e.g. "eq" } type HitDoc struct { - Index string `json:"_index"` //name of index - Type string `json:"_type"` //_doc + Index string `json:"_index"` // name of index + Type string `json:"_type"` // _doc ID string `json:"_id"` Score float64 `json:"_score"` // - Source map[string]interface{} `json:"_source"` //the document of itemType + Source map[string]interface{} `json:"_source"` // the document of itemType } type myDoc struct { diff --git a/responses/responses.go b/responses/responses.go index e53097136b01bc5879c98b72809dc5501ab9c2c9..0cec3b118b4f7021ac4961ba1bd5581078cd85c8 100644 --- a/responses/responses.go +++ b/responses/responses.go @@ -4,8 +4,8 @@ import ( "encoding/json" "net/http" - "gitlab.com/uafrica/go-utils/logs" - "gitlab.com/uafrica/go-utils/utils" + "gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/logs" + "gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/utils" "github.com/aws/aws-lambda-go/events" ) diff --git a/s3/s3.go b/s3/s3.go index 1aa35c03c176edf9dfd1227d5d5361a6c673fc76..43edf170586ad078af45f5476bb017ddef27e1a6 100644 --- a/s3/s3.go +++ b/s3/s3.go @@ -10,7 +10,7 @@ import ( "time" "github.com/aws/aws-sdk-go/aws/awserr" - "gitlab.com/uafrica/go-utils/errors" + "gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/errors" "github.com/aws/aws-sdk-go/aws/session" @@ -121,6 +121,7 @@ func (s SessionWithHelpers) UploadWithSettings(data []byte, bucket, fileName str Body: bytes.NewReader(data), } + // This sets the expiry date of the download link, not the deletion date of the object in the bucket. if settings.ExpiryDuration != nil { expiry := time.Now().Add(*settings.ExpiryDuration) putInput.Expires = &expiry diff --git a/search/config.go b/search/config.go index 1ae4c1974d40987d06945aaabeb9089e998cba90..4f1a112a7b1aa4b4ad621b8b2ddbf0073a22dacf 100644 --- a/search/config.go +++ b/search/config.go @@ -3,21 +3,21 @@ package search import ( "regexp" - "gitlab.com/uafrica/go-utils/errors" + "gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/errors" ) type Config struct { Addresses []string `json:"addresses" doc:"List of server addresses. Requires at least one, e.g. \"https://localhost:9200\" for local testing"` Username string `json:"username" doc:"User name for HTTP basic auth. Defaults to admin for local testing."` Password string `json:"password" doc:"User password for HTTP basic auth. Defaults to admin for local testing."` - //TLSCertificateFilename string `json:"tls_certificate_filename" doc:"Filename to load TLS certificate. Defaults to insecure connection when not specified."` - //IndexName string `json:"index_name" doc:"OpenSearch index name should be lowercase with dashes"` + // TLSCertificateFilename string `json:"tls_certificate_filename" doc:"Filename to load TLS certificate. Defaults to insecure connection when not specified."` + // IndexName string `json:"index_name" doc:"OpenSearch index name should be lowercase with dashes"` } func (c *Config) Validate() error { if len(c.Addresses) == 0 { return errors.Errorf("missing addresses") - //c.Addresses = []string{"https://localhost:9200"} + // c.Addresses = []string{"https://localhost:9200"} } if c.Username == "" { c.Username = "admin" diff --git a/search/document_store.go b/search/document_store.go index 9a6fb1c3518e6be0c9e8a3c1b6e5eff47b831548..581670324dfd32bfe6faee4547aa49eb9e754795 100644 --- a/search/document_store.go +++ b/search/document_store.go @@ -10,8 +10,8 @@ import ( "strings" opensearchapi "github.com/opensearch-project/opensearch-go/opensearchapi" - "gitlab.com/uafrica/go-utils/errors" - "gitlab.com/uafrica/go-utils/logs" + "gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/errors" + "gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/logs" ) type DocumentStore struct { diff --git a/search/time_series.go b/search/time_series.go index b6cb0d3422bdccfb92e2a9e22bec55a364a87f7a..8eb4d90eea38a5a41f326e17df33370e30c9501f 100644 --- a/search/time_series.go +++ b/search/time_series.go @@ -11,8 +11,8 @@ import ( "time" "github.com/opensearch-project/opensearch-go/opensearchapi" - "gitlab.com/uafrica/go-utils/errors" - "gitlab.com/uafrica/go-utils/logs" + "gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/errors" + "gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/logs" ) const TimeFormat = "2006-01-02T15:04:05Z07:00" diff --git a/search/writer.go b/search/writer.go index 72f9d24ce05e6182ea7c3b02a588ee843974fdab..6ba69f8dd950cd73bf02808ac4a5f44cf2095dfc 100644 --- a/search/writer.go +++ b/search/writer.go @@ -5,8 +5,8 @@ import ( "encoding/json" opensearch "github.com/opensearch-project/opensearch-go" opensearchapi "github.com/opensearch-project/opensearch-go/opensearchapi" - "gitlab.com/uafrica/go-utils/errors" - "gitlab.com/uafrica/go-utils/logs" + "gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/errors" + "gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/logs" "net/http" "strings" "time" diff --git a/secrets_manager/secrets_manager.go b/secrets_manager/secrets_manager.go index 3536167c1d78dc521f7daa18c20eb91867dba978..ca371ed263075000da2cdc2fe7e9a77a47be7424 100644 --- a/secrets_manager/secrets_manager.go +++ b/secrets_manager/secrets_manager.go @@ -4,8 +4,8 @@ import ( "encoding/base64" "os" - "gitlab.com/uafrica/go-utils/logs" - "gitlab.com/uafrica/go-utils/struct_utils" + "gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/logs" + "gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/struct_utils" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/awserr" diff --git a/sqs/sqs.go b/sqs/sqs.go index be6deb7cc036ff7ffa2f43aedf6d150d35ca476d..3275a67fd834dac6855f6d427f22bfbcd93f5380 100644 --- a/sqs/sqs.go +++ b/sqs/sqs.go @@ -6,14 +6,14 @@ import ( "encoding/json" "fmt" "github.com/google/uuid" - "gitlab.com/uafrica/go-utils/s3" + "gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/s3" "io/ioutil" "time" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/sqs" - "gitlab.com/uafrica/go-utils/logs" + "gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/logs" ) var sqsClient *sqs.SQS diff --git a/struct_utils/map_params_test.go b/struct_utils/map_params_test.go index a0d539e562cd65f831473c0231cdec542a7729a5..4f665515c849a9928b6781eb467a58b949dffc47 100644 --- a/struct_utils/map_params_test.go +++ b/struct_utils/map_params_test.go @@ -3,26 +3,26 @@ package struct_utils_test import ( "testing" - "gitlab.com/uafrica/go-utils/struct_utils" + "gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/struct_utils" ) func TestParams(t *testing.T) { type s struct { - NameWithoutTag string //will not be encoded into params! - NameWithDashTag string `json:"-"` //will not be encoded into params! - Name string `json:"name"` //encoded always - NameOmitempty string `json:"name2,omitempty"` //encoded when not empty + NameWithoutTag string // will not be encoded into params! + NameWithDashTag string `json:"-"` // will not be encoded into params! + Name string `json:"name"` // encoded always + NameOmitempty string `json:"name2,omitempty"` // encoded when not empty } ps := s{"a", "b", "c", "d"} pm := struct_utils.MapParams(ps) - if len(pm) != 2 || pm["name"] != "c" || pm["name2"] != "d" { //name2 is encoded when not empty + if len(pm) != 2 || pm["name"] != "c" || pm["name2"] != "d" { // name2 is encoded when not empty t.Fatalf("wrong params: %+v != %+v", ps, pm) } t.Logf("ps=%+v -> pm=%+v", ps, pm) ps = s{} pm = struct_utils.MapParams(ps) - if len(pm) != 1 || pm["name"] != "" { //name is always encoded because it has json tag and does not specify omitempty + if len(pm) != 1 || pm["name"] != "" { // name is always encoded because it has json tag and does not specify omitempty t.Fatalf("wrong params: %+v != %+v", ps, pm) } t.Logf("ps=%+v -> pm=%+v", ps, pm) diff --git a/struct_utils/named_values_to_struct.go b/struct_utils/named_values_to_struct.go index 68d8536b2fe451b87973cfa9166453c38131e939..3482c22c3b460a587e00c4adc39274031c108ea6 100644 --- a/struct_utils/named_values_to_struct.go +++ b/struct_utils/named_values_to_struct.go @@ -9,22 +9,22 @@ import ( "strconv" "strings" - "gitlab.com/uafrica/go-utils/errors" - "gitlab.com/uafrica/go-utils/logs" - "gitlab.com/uafrica/go-utils/string_utils" + "gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/errors" + "gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/logs" + "gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/string_utils" ) -//Purpose: +// Purpose: // Make a list of named values from the env for parsing into a struct // -//Parameters: +// Parameters: // prefix should be uppercase (by convention) env prefix like "MY_LIB_CONFIG", without trailing "_" // -//Result: +// Result: // named values that can be passed into UnmarshalNamedValues() // -//All env starting with "<prefix>_" will be copied without "<prefix>_" -//Examples with prefix="MY_LIB_CONFIG": +// All env starting with "<prefix>_" will be copied without "<prefix>_" +// Examples with prefix="MY_LIB_CONFIG": // MY_LIB_CONFIG_MAX_SIZE="6" -> {"MAX_SIZE":["6"]} one value of "6" // MY_LIB_CONFIG_NAMES ="A,B,C" -> {"NAMES":["A,B,C"]} one value of "A,B,C" // MY_LIB_CONFIG_NRS ="1,2,3" -> {"NRS":["1,2,3"]} one value of "1,2,3" (all env values are string, later parsed into int based on struct field type) @@ -54,17 +54,17 @@ func NamedValuesFromReader(prefix string, reader string_utils.KeyReader) map[str } result[strings.ToLower(key)] = []string{value} - //split only if valid CSV between [...] + // split only if valid CSV between [...] if value[0] == '[' && value[len(value)-1] == ']' { csvReader := csv.NewReader(strings.NewReader(value[1 : len(value)-1])) - csvValues, csvErr := csvReader.Read() //this automatically removes quotes around some/all CSV inside the [...] + csvValues, csvErr := csvReader.Read() // this automatically removes quotes around some/all CSV inside the [...] if csvErr == nil { result[strings.ToLower(key)] = csvValues } } } - //merge multiple <name>_#=<value> into single lists called <name> + // merge multiple <name>_#=<value> into single lists called <name> namesToDelete := []string{} merged := map[string][]nrWithValues{} for name, values := range result { @@ -82,15 +82,15 @@ func NamedValuesFromReader(prefix string, reader string_utils.KeyReader) map[str } } - //delete merged values + // delete merged values for _, name := range namesToDelete { delete(result, name) } - //sort and set the merged names with single list of values + // sort and set the merged names with single list of values for nameWithoutNr, nrsWithValues := range merged { if values, ok := result[nameWithoutNr]; ok { - nrsWithValues = append(nrsWithValues, nrWithValues{nr: 0, values: values}) //if also defined without _# + nrsWithValues = append(nrsWithValues, nrWithValues{nr: 0, values: values}) // if also defined without _# } sort.Slice(nrsWithValues, func(i, j int) bool { return nrsWithValues[i].nr < nrsWithValues[j].nr }) @@ -108,8 +108,8 @@ type nrWithValues struct { values []string } -//converts query string params to named values that can be parsed into a struct -//it support both single/multi-value params, depending how you get them from your HTTP library +// converts query string params to named values that can be parsed into a struct +// it support both single/multi-value params, depending how you get them from your HTTP library // (e.g. AWS API Gateway Context returns both but default golang net/http returns only params) func NamedValuesFromURL(params map[string]string, multiValueParams map[string][]string) map[string][]string { result := map[string][]string{} @@ -120,7 +120,7 @@ func NamedValuesFromURL(params map[string]string, multiValueParams map[string][] if list, ok := result[n]; !ok { result[n] = mv } else { - //do not add duplicates - seems like AWS put same value in both single and multivalue params + // do not add duplicates - seems like AWS put same value in both single and multivalue params for _, v := range mv { found := false for _, existingValue := range list { @@ -139,20 +139,20 @@ func NamedValuesFromURL(params map[string]string, multiValueParams map[string][] for name, values := range result { splitValues := []string{} for _, value := range values { - //split only if valid CSV between [...] + // split only if valid CSV between [...] if value == "" { continue } if value[0] == '[' && value[len(value)-1] == ']' { csvReader := csv.NewReader(strings.NewReader(value[1 : len(value)-1])) - csvValues, csvErr := csvReader.Read() //this automatically removes quotes around some/all CSV inside the [...] + csvValues, csvErr := csvReader.Read() // this automatically removes quotes around some/all CSV inside the [...] if csvErr == nil { splitValues = append(splitValues, csvValues...) } else { - splitValues = append(splitValues, value) //cannot split this "[...]" value + splitValues = append(splitValues, value) // cannot split this "[...]" value } } else { - splitValues = append(splitValues, value) //not a "[...]" value + splitValues = append(splitValues, value) // not a "[...]" value } } result[name] = splitValues @@ -216,7 +216,7 @@ func unmarshalNamedValuesIntoStructPtr(prefix string, namedValues map[string][]s structTypeField := structType.Field(i) fieldName := (strings.SplitN(structTypeField.Tag.Get("json"), ",", 2))[0] if fieldName == "-" { - continue //skip fields excluded from JSON + continue // skip fields excluded from JSON } if prefix != "" { if fieldName != "" { @@ -226,7 +226,7 @@ func unmarshalNamedValuesIntoStructPtr(prefix string, namedValues map[string][]s } } - //recurse into anonymous sub-structs + // recurse into anonymous sub-structs if structTypeField.Type.Kind() == reflect.Struct { if nameList, err := unmarshalNamedValuesIntoStructPtr(fieldName, namedValues, structTypeField.Type, structPtrValue.Elem().Field(i).Addr()); err != nil { return nil, errors.Wrapf(err, "failed on %s.%s", structType.Name(), fieldName) @@ -238,24 +238,24 @@ func unmarshalNamedValuesIntoStructPtr(prefix string, namedValues map[string][]s fieldValues, ok := namedValues[fieldName] if !ok { - continue //skip undefined fields + continue // skip undefined fields } usedNameList = append(usedNameList, fieldName) delete(namedValues, fieldName) if len(fieldValues) == 0 { - continue //field has no value specified in URL, do not remove values not defined (cannot clear defined struct fields with named values) + continue // field has no value specified in URL, do not remove values not defined (cannot clear defined struct fields with named values) } structPtrFieldValue := structPtrValue.Elem().Field(i) if structPtrFieldValue.Kind() == reflect.Ptr { - //this is a ptr, allocate a new value and set it - //then we can dereference to set it below + // this is a ptr, allocate a new value and set it + // then we can dereference to set it below structPtrFieldValue.Set(reflect.New(structPtrFieldValue.Type().Elem())) structPtrFieldValue = structPtrFieldValue.Elem() } - //param is defined >=1 times in URL + // param is defined >=1 times in URL if structTypeField.Type.Kind() == reflect.Slice { - //this param struct field is a slice, iterate over all specified values + // this param struct field is a slice, iterate over all specified values for i, fieldValue := range fieldValues { parsedValue, parseErr := unmarshalValue(fieldValue, structTypeField.Type.Elem()) if parseErr != nil { @@ -264,10 +264,10 @@ func unmarshalNamedValuesIntoStructPtr(prefix string, namedValues map[string][]s } structPtrFieldValue.Set(reflect.Append(structPtrFieldValue, parsedValue)) - //todo: sorting of list using value names as applicable + // todo: sorting of list using value names as applicable } } else { - //field is not a slice, expecting only a single value + // field is not a slice, expecting only a single value if len(fieldValues) > 1 { err = errors.Errorf("%s cannot store multiple value (%d found: %+v)", fieldName, len(fieldValues), fieldValues) return @@ -279,37 +279,37 @@ func unmarshalNamedValuesIntoStructPtr(prefix string, namedValues map[string][]s } structPtrFieldValue.Set(parsedValue) } - } //for each param struct field + } // for each param struct field return usedNameList, nil } func unmarshalValue(v interface{}, t reflect.Type) (reflect.Value, error) { newValuePtr := reflect.New(t) if reflect.ValueOf(v).Type().AssignableTo(t) { - newValuePtr.Elem().Set(reflect.ValueOf(v)) //can assign as is + newValuePtr.Elem().Set(reflect.ValueOf(v)) // can assign as is return newValuePtr.Elem(), nil } - //needs conversion + // needs conversion s, ok := v.(string) if !ok { jsonValue, _ := json.Marshal(v) s = string(jsonValue) } - //now we have string value + // now we have string value if valueScanner, ok := newValuePtr.Interface().(sql.Scanner); ok { - //if has scanner - prefer that over json unmarshal - //because we do not know if json expects quoted/unquoted for this type - //and if we try quoted, it fail, then try unquoted, we have two different - //errors one one of them will be kind of meaning nothing, but which? - //scanner should always take the value as typed, so let's try that first + // if has scanner - prefer that over json unmarshal + // because we do not know if json expects quoted/unquoted for this type + // and if we try quoted, it fail, then try unquoted, we have two different + // errors one one of them will be kind of meaning nothing, but which? + // scanner should always take the value as typed, so let's try that first if err := valueScanner.Scan(s); err == nil { return newValuePtr.Elem(), nil } } - //try JSON unmarshal as is else with quotes + // try JSON unmarshal as is else with quotes if err := json.Unmarshal([]byte(s), newValuePtr.Interface()); err == nil { return newValuePtr.Elem(), nil } diff --git a/struct_utils/named_values_to_struct_test.go b/struct_utils/named_values_to_struct_test.go index b04872556905c9249581c67239f76c586707e23c..653cb0256d48ceea4db3da4690e09e0e18e8b767 100644 --- a/struct_utils/named_values_to_struct_test.go +++ b/struct_utils/named_values_to_struct_test.go @@ -9,25 +9,25 @@ import ( "testing" "time" - "gitlab.com/uafrica/go-utils/errors" - "gitlab.com/uafrica/go-utils/logs" - "gitlab.com/uafrica/go-utils/struct_utils" + "gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/errors" + "gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/logs" + "gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/struct_utils" ) func TestEnv(t *testing.T) { logs.SetGlobalFormat(logs.NewConsole()) logs.SetGlobalLevel(logs.LevelDebug) - //booleans + // booleans os.Setenv("TEST_VALUE_ENABLE_CACHE", "true") os.Setenv("TEST_VALUE_DISABLE_LOG", "true") os.Setenv("TEST_VALUE_ADMIN", "false") - //integers + // integers os.Setenv("TEST_VALUE_MAX_SIZE", "12") - os.Setenv("TEST_VALUE_SEQ1", "[4,5,6]") //list in one value + os.Setenv("TEST_VALUE_SEQ1", "[4,5,6]") // list in one value - os.Setenv("TEST_VALUE_SEQ2_10", "10") //numbered list elements + os.Setenv("TEST_VALUE_SEQ2_10", "10") // numbered list elements os.Setenv("TEST_VALUE_SEQ2_20", "20") os.Setenv("TEST_VALUE_SEQ2_4", "4") os.Setenv("TEST_VALUE_SEQ2_15", "15") @@ -36,7 +36,7 @@ func TestEnv(t *testing.T) { os.Setenv("TEST_VALUE_CUTOFF", "2021-11-20T12:00:00+02:00") os.Setenv("TEST_VALUE_HOLIDAYS", "[2021-03-21,2021-04-27,2021-05-01,2021-06-16,2021-08-09,2021-12-16,2021-12-25]") - //=====[ TEST THIS FUNCTION ]===== + // =====[ TEST THIS FUNCTION ]===== nv := struct_utils.NamedValuesFromEnv("TEST_VALUE") testNamedValues(t, nv) @@ -52,12 +52,12 @@ func TestURL1(t *testing.T) { "admin": "false", "max_size": "12", "seq1": "[4,5,6]", - "seq2": "[100,4,10,15,20]", //url does not support _# numbering of params in a list, only [csv] or multi-value with seq2=100&seq2=4&... testing in TestURL2() + "seq2": "[100,4,10,15,20]", // url does not support _# numbering of params in a list, only [csv] or multi-value with seq2=100&seq2=4&... testing in TestURL2() "cutoff": "2021-11-20T12:00:00+02:00", "holidays": "[2021-03-21,2021-04-27,2021-05-01,2021-06-16,2021-08-09,2021-12-16,2021-12-25]", } - //=====[ TEST THIS FUNCTION ]===== + // =====[ TEST THIS FUNCTION ]===== nv := struct_utils.NamedValuesFromURL(queryParams, nil) testNamedValues(t, nv) } @@ -75,12 +75,12 @@ func TestURL2(t *testing.T) { } multiValueParams := map[string][]string{ "enable_cache": {"true"}, - "seq1": {"5", "6"}, //merged with above "4" + "seq1": {"5", "6"}, // merged with above "4" "seq2": {"100", "4", "10", "15", "20"}, "holidays": {"2021-03-21", "2021-04-27", "2021-05-01", "2021-06-16", "2021-08-09", "2021-12-16", "2021-12-25"}, } - //=====[ TEST THIS FUNCTION ]===== + // =====[ TEST THIS FUNCTION ]===== nv := struct_utils.NamedValuesFromURL(queryParams, multiValueParams) testNamedValues(t, nv) } @@ -96,14 +96,14 @@ func TestURL3(t *testing.T) { } func testNamedValues(t *testing.T, nv map[string][]string) { - //assets expected values + // assets expected values exp := map[string][]string{ "enable_cache": {"true"}, "disable_log": {"true"}, "admin": {"false"}, "max_size": {"12"}, "seq1": {"4", "5", "6"}, - "seq2": {"100", "4", "10", "15", "20"}, //order is important + "seq2": {"100", "4", "10", "15", "20"}, // order is important "cutoff": {"2021-11-20T12:00:00+02:00"}, "holidays": {"2021-03-21", "2021-04-27", "2021-05-01", "2021-06-16", "2021-08-09", "2021-12-16", "2021-12-25"}, } @@ -126,7 +126,7 @@ func testNamedValues(t *testing.T, nv map[string][]string) { } } - //=====[ PARSE INTO STRUCT ]========== + // =====[ PARSE INTO STRUCT ]========== c := Config{} unused, err := struct_utils.UnmarshalNamedValues(nv, &c) if err != nil { diff --git a/utils/utils.go b/utils/utils.go index 7ad02bb22dde9973a52b8971599452e14ec2dde9..f859bd0fda5cf3a6729bb64dd53ce07da6e4054d 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -11,7 +11,7 @@ import ( "strings" "time" - "gitlab.com/uafrica/go-utils/struct_utils" + "gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/struct_utils" ) // GetEnv is a helper function for getting environment variables with a default