Skip to content

Commit

Permalink
re-implement Parameters Read() function (#23)
Browse files Browse the repository at this point in the history
* re-implement Parameters Read() function

* go fmt

* address PR comments
  • Loading branch information
rlankfo authored Jun 25, 2020
1 parent 4975880 commit 3e971bd
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 4 deletions.
21 changes: 17 additions & 4 deletions parameter.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,30 @@ func NewParameters(basePath string, parameters map[string]*Parameter) *Parameter

//Parameters holds the output and all AWS Parameter Store that have the same base path
type Parameters struct {
readIndex int64
bytesJSON []byte
basePath string
parameters map[string]*Parameter
}

//Read implements the io.Reader interface for the key/value pair
func (p *Parameters) Read(des []byte) (n int, err error) {
bytesJSON, err := json.Marshal(p.getKeyValueMap())
if err != nil {
return 0, err
if p.bytesJSON == nil {
p.bytesJSON, err = json.Marshal(p.getKeyValueMap())
if err != nil {
return 0, err
}
}
return copy(des, bytesJSON), io.EOF

if p.readIndex >= int64(len(p.bytesJSON)) {
p.readIndex = 0
return 0, io.EOF
}

n = copy(des, p.bytesJSON[p.readIndex:])
p.readIndex += int64(n)

return n, nil
}

//GetValueByName returns the value based on the name
Expand Down
62 changes: 62 additions & 0 deletions parameter_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package awsssm

import (
"bytes"
"math/rand"
"reflect"
"testing"
"time"
)

type env struct {
Expand Down Expand Up @@ -113,9 +116,68 @@ func TestParameters_Decode(t *testing.T) {
}
}

func TestParameters_Read(t *testing.T) {
tests := []struct {
name string
basePath string
parameters map[string]*Parameter
expectedError error
expectedByteCount int64
}{
{
name: "ReadFrom Small Byte Count",
basePath: "/my-service/dev",
parameters: getParametersMap(),
expectedError: nil,
expectedByteCount: 70,
},
{
name: "ReadFrom Large Byte Count",
basePath: "/my-service/dev",
parameters: getRandomParametersMap(1000, 10),
expectedError: nil,
expectedByteCount: 33001,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
parameter := NewParameters(test.basePath, test.parameters)
buf := new(bytes.Buffer)
bytesRead, err := buf.ReadFrom(parameter)
if err != test.expectedError {
t.Errorf(`Unexpected error: got %s, expected %s`, err, test.expectedError)
}
if bytesRead != test.expectedByteCount {
t.Errorf(`Unexpected value: got %d, expected %d`, bytesRead, test.expectedByteCount)
}
})
}
}

func getParametersMap() map[string]*Parameter {
return map[string]*Parameter{
"/my-service/dev/DB_PASSWORD": {Value: param1.Value},
"/my-service/dev/DB_HOST": {Value: param2.Value},
}
}

var seededRand = rand.New(
rand.NewSource(time.Now().UnixNano()))

const charset = "abcdefghijklmnopqrstuvwxyz" +
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"

func getRandomParametersMap(n int, l int) map[string]*Parameter {
m := make(map[string]*Parameter)
for i := 0; i < n; i++ {
s := "/my-service/dev/"
for j := 0; j < l; j++ {
char := charset[seededRand.Intn(int(^uint(0)>>1))%len(charset)]
s += string(char)
}
m[s] = &Parameter{
Value: param1.Value,
}
}
return m
}

0 comments on commit 3e971bd

Please sign in to comment.