Skip to content

Commit

Permalink
Add utility functions
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-sumi-k committed Aug 9, 2023
1 parent ab21dcd commit ab82265
Show file tree
Hide file tree
Showing 10 changed files with 145 additions and 5 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ jobs:
run: |
cd file && go test . && cd ..
cd email && go test . && cd ..
cd jwtAuth && go test . && cd ..
cd jwtAuth && go test . && cd ..
cd util && go test . && cd ..
4 changes: 0 additions & 4 deletions examples/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@ module examples

go 1.20

replace email => ../email

replace file => ../file

require (
github.com/canopas/go-reusables/email v0.0.0-20230801123715-3957df64671f
github.com/canopas/go-reusables/file v0.0.0-20230801123715-3957df64671f
Expand Down
10 changes: 10 additions & 0 deletions examples/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package examples

import "github.com/canopas/go-reusables/util"


func ExamplesUtils(){
util.GenerateOTP(4)
util.LCM([]int{20, 45, 68})
util.GCD(124, 674)
}
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
module github.com/canopas/go-reusables

go 1.20

require github.com/sirupsen/logrus v1.9.3

require golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
11 changes: 11 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
13 changes: 13 additions & 0 deletions util/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Utils

This module contains utility examples for golang

## Utilities

- Generate OTP for any length
- Get LCM of given numbers array
- Get GCD of given numbers

## Example

Find a working example of Utils-reusable at [Example](https://github.com/canopas/go-reusables/blob/main/examples/util.go).
11 changes: 11 additions & 0 deletions util/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module github.com/canopas/go-reusables/util

go 1.20

require github.com/stretchr/testify v1.8.4

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
10 changes: 10 additions & 0 deletions util/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
54 changes: 54 additions & 0 deletions util/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package util

import (
random "crypto/rand"
"errors"
"io"
)

// generate OTP of given length with numbers only
var table = [...]byte{'1', '2', '3', '4', '5', '6', '7', '8', '9', '0'}

func GenerateOTP(length int) (string, error) {

if length <= 0{
return "", errors.New("Enter valid length for OTP")
}

randomNumberBuf := make([]byte, length)

_, err := io.ReadAtLeast(random.Reader, randomNumberBuf, length)
if err != nil {
return "", err
}

for i := 0; i < len(randomNumberBuf); i++ {
randomNumberBuf[i] = table[int(randomNumberBuf[i])%len(table)]
}

return string(randomNumberBuf), nil
}

// Find GCD(greatest common divisor) of given numbers
func GCD(a, b int) int {
for b != 0 {
t := b
b = a % b
a = t
}
return a
}

// Find LCM(Least Common Multiple) of the list
func LCM(list []int) int {
if len(list) > 0 {
if len(list) == 1 {
return list[0]
}
if len(list) == 2 {
return list[0] * list[1] / GCD(list[0], list[1])
}
return LCM([]int{list[0], LCM(list[1:])})
}
return 0
}
30 changes: 30 additions & 0 deletions util/util_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package util

import (
"github.com/stretchr/testify/assert"
"testing"
)

func TestGenerateOTPError(t *testing.T) {
asserts := assert.New(t)
temp, err := GenerateOTP(0)
asserts.Error(err)
asserts.Empty(temp)
}

func TestGenerateOTPSuccess(t *testing.T) {
asserts := assert.New(t)
temp, err := GenerateOTP(4)
asserts.NoError(err)
asserts.NotEmpty(temp)
}

func TestLCM(t *testing.T) {
asserts := assert.New(t)
asserts.Equal(40, LCM([]int{4, 8, 20}))
}

func TestGCD(t *testing.T) {
asserts := assert.New(t)
asserts.Equal(6, GCD(36, 30))
}

0 comments on commit ab82265

Please sign in to comment.