Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
bobgroup-go-utils
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Bob Public Utils
bobgroup-go-utils
Commits
bf093221
Commit
bf093221
authored
1 year ago
by
Johan de Klerk
Browse files
Options
Downloads
Plain Diff
Merge branch 'main' of gitlab.bob.co.za:bob-public-utils/bobgroup-go-utils
parents
6b24b1db
6b2a0a5f
No related branches found
No related tags found
No related merge requests found
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
audit/audit.go
+69
-0
69 additions, 0 deletions
audit/audit.go
date_utils/date_utils.go
+1
-1
1 addition, 1 deletion
date_utils/date_utils.go
responses/responses.go
+9
-1
9 additions, 1 deletion
responses/responses.go
utils/utils.go
+26
-0
26 additions, 0 deletions
utils/utils.go
with
105 additions
and
2 deletions
audit/audit.go
+
69
−
0
View file @
bf093221
...
...
@@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"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/number_utils"
"reflect"
"regexp"
...
...
@@ -39,6 +40,10 @@ func VerifyAuditEvents(original interface{}, new interface{}) error {
}
func
GetChanges
(
original
interface
{},
new
interface
{})
(
map
[
string
]
interface
{},
error
)
{
// Clean audit events
original
=
cleanStruct
(
original
)
new
=
cleanStruct
(
new
)
changes
:=
map
[
string
]
interface
{}{}
changelog
,
err
:=
diff
.
Diff
(
original
,
new
)
if
err
!=
nil
{
...
...
@@ -128,6 +133,70 @@ func GetChanges(original interface{}, new interface{}) (map[string]interface{},
return
changes
,
nil
}
func
cleanStruct
(
object
interface
{})
interface
{}
{
defer
func
()
{
if
err
:=
recover
();
err
!=
nil
{
logs
.
ErrorMsg
(
fmt
.
Sprintf
(
"audit event panic: %+v"
,
err
))
}
}()
// If the object is empty, we have nothing to do
if
object
==
nil
{
return
object
}
// Convert the object to a pointer
if
reflect
.
ValueOf
(
object
)
.
Kind
()
!=
reflect
.
Ptr
{
val
:=
reflect
.
ValueOf
(
object
)
// Create a new pointer to a new value of the type of the object
ptr
:=
reflect
.
New
(
reflect
.
TypeOf
(
object
))
// Set the newly created pointer to point to the object
ptr
.
Elem
()
.
Set
(
val
)
// Overwrite the original object
object
=
ptr
.
Interface
()
}
// Get the value of the object
val
:=
reflect
.
ValueOf
(
object
)
if
val
.
Kind
()
==
reflect
.
Ptr
{
val
=
val
.
Elem
()
}
// We can only clean structs
if
val
.
Kind
()
!=
reflect
.
Struct
{
return
object
}
// Loop through the field tags to see if we should include the related object or not.
// We default to exclude, unless specified to include
for
i
:=
0
;
i
<
val
.
NumField
();
i
++
{
fieldVal
:=
val
.
Field
(
i
)
structField
:=
val
.
Type
()
.
Field
(
i
)
// Determine whether the field should be included or excluded
value
,
_
:=
structField
.
Tag
.
Lookup
(
"audit"
)
shouldIncludeForAudit
:=
value
==
"true"
shouldExcludeForAudit
:=
value
==
"false"
// If the audit tag is present and specified to 'true', we should always include the relation
if
shouldIncludeForAudit
{
continue
}
// By default, all bun relations are excluded
isBunRelationField
:=
strings
.
Contains
(
structField
.
Tag
.
Get
(
"bun"
),
"rel:"
)
if
shouldExcludeForAudit
||
isBunRelationField
{
if
fieldVal
.
CanSet
()
{
// Set the field to its zero value (nil for pointers)
fieldVal
.
Set
(
reflect
.
Zero
(
fieldVal
.
Type
()))
}
}
}
return
object
}
func
ChildObjectChanges
(
changes
map
[
string
]
interface
{},
objectPath
string
,
fieldPath
string
,
changeFrom
interface
{},
changeTo
interface
{})
{
objectKey
:=
ToSnakeCase
(
objectPath
)
...
...
This diff is collapsed.
Click to expand it.
date_utils/date_utils.go
+
1
−
1
View file @
bf093221
...
...
@@ -46,7 +46,7 @@ func DateLayoutYearMonthDayTime() string {
}
func
DateLayoutFilenameSafe
()
string
{
layout
:=
"
2006-01-02(
15h04
s05)
"
layout
:=
"
02-Jan-2006-
15h04"
return
layout
}
...
...
This diff is collapsed.
Click to expand it.
responses/responses.go
+
9
−
1
View file @
bf093221
...
...
@@ -2,9 +2,10 @@ package responses
import
(
"encoding/json"
"gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/map_utils"
"net/http"
"gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/map_utils"
"gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/logs"
"gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/utils"
...
...
@@ -116,3 +117,10 @@ func OptionsResponse() events.APIGatewayProxyResponse {
Headers
:
map_utils
.
MergeMaps
(
utils
.
CorsHeaders
(),
ContentTypeJSONHeader
),
}
}
func
MovedPermanentlyResponse
(
url
string
)
events
.
APIGatewayProxyResponse
{
return
events
.
APIGatewayProxyResponse
{
StatusCode
:
http
.
StatusMovedPermanently
,
Headers
:
map_utils
.
MergeMaps
(
utils
.
CorsHeaders
(),
map
[
string
]
string
{
"Location"
:
url
}),
}
}
This diff is collapsed.
Click to expand it.
utils/utils.go
+
26
−
0
View file @
bf093221
...
...
@@ -67,6 +67,32 @@ func ValidateEmailAddress(email string) (string, error) {
return
cleanEmail
,
nil
}
func
StripEmail
(
email
string
)
(
strippedEmail
string
,
strippedDomain
string
)
{
// Strip the email address from the + to the @
// Define a regular expression pattern to match the "+" to "@" part
emailPattern
:=
`(\+.*@)`
// Define the regular expression pattern to match the domain part after "@"
domainPattern
:=
`@(.+)`
// Compile the regular expression
emailRegex
:=
regexp
.
MustCompile
(
emailPattern
)
domainRegex
:=
regexp
.
MustCompile
(
domainPattern
)
// Replace the matched part with an empty string
strippedEmail
=
emailRegex
.
ReplaceAllString
(
email
,
"@"
)
// Find the first match in the email address
match
:=
domainRegex
.
FindStringSubmatch
(
email
)
// Check if a match was found
if
len
(
match
)
>
1
{
// The domain part (excluding "@") is in the first capture group (index 1)
strippedDomain
=
match
[
1
]
}
return
strippedEmail
,
strippedDomain
}
// IsUrlStrict Returns whether a URL is valid in a strict way (Must have scheme and host)
func
IsUrlStrict
(
str
string
)
bool
{
u
,
err
:=
url
.
Parse
(
str
)
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment