Skip to content

Commit b7702a7

Browse files
committed
add new AWS helper module, pointer helpers, and split the bytes package into separate files within for readability
1 parent 1407345 commit b7702a7

442 files changed

Lines changed: 104779 additions & 174 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

aws/aws.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package aws
2+
3+
import (
4+
"context"
5+
"github.com/aws/aws-sdk-go-v2/aws"
6+
"github.com/aws/aws-sdk-go-v2/config"
7+
"log"
8+
)
9+
10+
type AWS struct {
11+
context context.Context
12+
config aws.Config
13+
}
14+
15+
// New creates a new AWS config using the context and region specified
16+
// If you want an empty context, set ctx to context.TODO()
17+
// region should be an AWS region, like "us-west-2"
18+
func New(ctx context.Context, region string) *AWS {
19+
// Using the SDK's default configuration, loading additional config
20+
// and credentials values from the environment variables, shared
21+
// credentials, and shared configuration files
22+
cfg, err := config.LoadDefaultConfig(ctx, config.WithRegion(region))
23+
if err != nil {
24+
log.Fatalf("unable to load SDK config, %v", err)
25+
}
26+
return &AWS{
27+
context: ctx,
28+
config: cfg,
29+
}
30+
}

aws/services.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package aws
2+
3+
import (
4+
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
5+
"github.com/aws/aws-sdk-go-v2/service/secretsmanager"
6+
)
7+
8+
// NewDynamo creates and returns a new instance of dynamo db
9+
func (a *AWS) NewDynamo() *dynamodb.Client {
10+
return dynamodb.NewFromConfig(a.config)
11+
}
12+
13+
// NewSecretsManager creates and returns a new instance of dynamo db
14+
func (a *AWS) NewSecretsManager(optFns ...func(*secretsmanager.Options)) *secretsmanager.Client {
15+
return secretsmanager.NewFromConfig(a.config, optFns...)
16+
}

byte/bytes.go

Lines changed: 6 additions & 169 deletions
Original file line numberDiff line numberDiff line change
@@ -1,126 +1,10 @@
11
package byte
22

33
import (
4-
"encoding/json"
5-
"errors"
64
"fmt"
7-
"regexp"
85
"strconv"
9-
"strings"
106
)
117

12-
// ParseErr returned invalid strings are parsed
13-
type ParseErr struct {
14-
Func string
15-
Str string
16-
Err error
17-
}
18-
19-
func (e *ParseErr) String() string {
20-
marshaledStruct, err := json.Marshal(e)
21-
if err != nil {
22-
return err.Error()
23-
}
24-
return string(marshaledStruct)
25-
}
26-
27-
func (e *ParseErr) Error() string {
28-
return "bytes." + e.Func + ": " + "parsing " + strconv.Quote(e.Str) + ": " + e.Err.Error()
29-
}
30-
31-
// BytesUnit represents a unit of magnitude for Bytes in either SI (base-10) or NIST (base-2)
32-
type BytesUnit int64
33-
34-
const (
35-
// B byte (Unit)
36-
B BytesUnit = 1
37-
// KB Kilobyte (SI) - 1000 bytes
38-
KB BytesUnit = 1000
39-
// MB Megabyte (SI) - 1e6 bytes
40-
MB BytesUnit = 1e6
41-
// GB Gigabyte (SI) - 1e9 bytes
42-
GB BytesUnit = 1e9
43-
// TB Terabyte (SI) - 1e12 bytes
44-
TB BytesUnit = 1e12
45-
// PB Petabyte (SI) - 1e15 bytes
46-
PB BytesUnit = 1e15
47-
48-
// KiB Kibibyte (NIST) - 1024 bytes
49-
KiB BytesUnit = 1024
50-
// MiB Mebibyte (NIST) - 2^20 bytes
51-
MiB = 1024 * KiB
52-
// GiB Gibibyte (NIST) - 2^30 bytes
53-
GiB = 1024 * MiB
54-
// TiB Tebibyte (NIST) - 2^40 bytes
55-
TiB = 1024 * GiB
56-
// PiB Pebibyte (NIST) - 2^50 bytes
57-
PiB = 1024 * TiB
58-
)
59-
60-
var strToByteUnit = map[string]BytesUnit{
61-
"": B,
62-
"B": B,
63-
"KB": KB,
64-
"KIB": KiB,
65-
"MB": MB,
66-
"MIB": MiB,
67-
"GB": GB,
68-
"GIB": GiB,
69-
"TB": TB,
70-
"TIB": TiB,
71-
"PB": PB,
72-
"PIB": PiB,
73-
}
74-
75-
var byteUnitToStr = map[BytesUnit]string{
76-
B: "B",
77-
KB: "KB",
78-
KiB: "KiB",
79-
MB: "MB",
80-
MiB: "MiB",
81-
GB: "GB",
82-
GiB: "GiB",
83-
TB: "TB",
84-
TiB: "TiB",
85-
PB: "PB",
86-
PiB: "PiB",
87-
}
88-
89-
var bytesUnitThresholds = []struct {
90-
unit BytesUnit
91-
less Bytes
92-
}{
93-
{B, 0x400},
94-
{KiB, Bytes(uint64(MiB) >> 4)},
95-
{MiB, Bytes(uint64(GiB) >> 4)},
96-
{GiB, Bytes(uint64(TiB) >> 4)},
97-
{TiB, Bytes(uint64(PiB) >> 4)},
98-
{PiB, 0x7FFFFFFFFFFFFFFF},
99-
}
100-
var bytesSIUnitThresholds = []struct {
101-
unit BytesUnit
102-
less Bytes
103-
}{
104-
{B, 5e2},
105-
{KB, 5e5},
106-
{MB, 5e8},
107-
{GB, 5e11},
108-
{TB, 5e14},
109-
{PB, 0x7FFFFFFFFFFFFFFF},
110-
}
111-
112-
// Convert a byte count into the (possibly fractional) count in this unit
113-
func (u BytesUnit) Convert(b Bytes) float64 {
114-
if u == B {
115-
return float64(b)
116-
}
117-
return float64(b) / float64(u)
118-
}
119-
120-
func (u BytesUnit) String() string {
121-
return byteUnitToStr[u]
122-
}
123-
1248
// Bytes is a formattable byte count
1259
type Bytes int64
12610

@@ -132,20 +16,20 @@ func (b Bytes) Format(s fmt.State, verb rune) {
13216
case 's':
13317
fallthrough
13418
case 'v':
135-
if b < 0 {
19+
bCopy := b
20+
if bCopy < 0 {
13621
_, _ = s.Write([]byte{'-'})
137-
b = b * -1
22+
bCopy = b * -1
13823
}
13924
thresholds := bytesUnitThresholds
14025
if s.Flag('+') {
14126
thresholds = bytesSIUnitThresholds
14227
}
14328
for _, t := range thresholds {
144-
if b < t.less {
145-
f := t.unit.Convert(b)
29+
if bCopy < t.less {
30+
f := t.unit.Convert(bCopy)
14631
if prec, ok := s.Precision(); ok {
147-
format := fmt.Sprintf("%%.%df %%s", prec)
148-
_, _ = fmt.Fprintf(s, format, f, t.unit)
32+
_, _ = fmt.Fprintf(s, fmt.Sprintf("%%.%df %%s", prec), f, t.unit)
14933
} else {
15034
_, _ = fmt.Fprintf(s, "%s %s", strconv.FormatFloat(f, 'f', -1, 64), t.unit)
15135
}
@@ -158,50 +42,3 @@ func (b Bytes) Format(s fmt.State, verb rune) {
15842
func (b Bytes) String() string {
15943
return fmt.Sprintf("%s", b)
16044
}
161-
162-
var parseBytesRegex = regexp.MustCompile(`(?i)(?P<value>[0-9.+eE+-]+)\s*(?P<unit>[a-z]?(?:i?b)?)?`)
163-
164-
// ParseBytes parses the given string with byte units into a Byte count
165-
func ParseBytes(s string) (Bytes, error) {
166-
gs := parseBytesRegex.FindStringSubmatch(s)
167-
if len(gs) == 0 {
168-
return 0, &ParseErr{
169-
Func: "ParseBytes",
170-
Str: s,
171-
Err: errors.New("bad string format"),
172-
}
173-
}
174-
vs := strings.ToLower(gs[1])
175-
us := gs[2]
176-
177-
unit, ok := strToByteUnit[strings.ToUpper(strings.TrimSpace(us))]
178-
if !ok {
179-
return 0, &ParseErr{
180-
Func: "ParseBytes",
181-
Str: s,
182-
Err: errors.New("unknown bytes unit"),
183-
}
184-
}
185-
186-
vs = strings.Replace(vs, ",", "", -1)
187-
if strings.Contains(vs, ".") || strings.Contains(vs, "e") {
188-
f, err := strconv.ParseFloat(vs, 64)
189-
if err != nil {
190-
return 0, &ParseErr{
191-
Func: "ParseBytes",
192-
Str: s,
193-
Err: err,
194-
}
195-
}
196-
return Bytes(f * float64(unit)), nil
197-
}
198-
i, err := strconv.ParseInt(vs, 10, 64)
199-
if err != nil {
200-
return 0, &ParseErr{
201-
Func: "ParseBytes",
202-
Str: s,
203-
Err: err,
204-
}
205-
}
206-
return Bytes(i * int64(unit)), nil
207-
}

byte/bytes_unit.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package byte
2+
3+
// BytesUnit represents a unit of magnitude for Bytes in either SI (base-10) or NIST (base-2)
4+
type BytesUnit int64
5+
6+
// Convert a byte count into the (possibly fractional) count in this unit
7+
func (u BytesUnit) Convert(b Bytes) float64 {
8+
if u == B {
9+
return float64(b)
10+
}
11+
return float64(b) / float64(u)
12+
}
13+
14+
func (u BytesUnit) String() string {
15+
return byteUnitToStr[u]
16+
}

byte/parse.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package byte
2+
3+
import (
4+
"errors"
5+
"regexp"
6+
"strconv"
7+
"strings"
8+
)
9+
10+
// ParseErr returned invalid strings are parsed
11+
type ParseErr struct {
12+
Func string
13+
Str string
14+
Err error
15+
}
16+
17+
func (e *ParseErr) Error() string {
18+
return e.String()
19+
}
20+
21+
func (e *ParseErr) String() string {
22+
return "bytes." + e.Func + ": " + "parsing " + strconv.Quote(e.Str) + ": " + e.Err.Error()
23+
}
24+
25+
// ParseBytes parses the given string with byte units into a Byte count
26+
func ParseBytes(s string) (Bytes, error) {
27+
regex := `(?i)(?P<value>[0-9.+eE+-]+)\s*(?P<unit>[a-z]?(?:i?b)?)?`
28+
gs := regexp.MustCompile(regex).FindStringSubmatch(s)
29+
if len(gs) == 0 {
30+
return 0, &ParseErr{
31+
Func: "ParseBytes",
32+
Str: s,
33+
Err: errors.New("bad string format"),
34+
}
35+
}
36+
vs := strings.ToLower(gs[1])
37+
us := gs[2]
38+
39+
unit, ok := strToByteUnit[strings.ToUpper(strings.TrimSpace(us))]
40+
if !ok {
41+
return 0, &ParseErr{
42+
Func: "ParseBytes",
43+
Str: s,
44+
Err: errors.New("unknown bytes unit"),
45+
}
46+
}
47+
48+
vs = strings.Replace(vs, ",", "", -1)
49+
if strings.Contains(vs, ".") || strings.Contains(vs, "e") {
50+
f, err := strconv.ParseFloat(vs, 64)
51+
if err != nil {
52+
return 0, &ParseErr{
53+
Func: "ParseBytes",
54+
Str: s,
55+
Err: err,
56+
}
57+
}
58+
return Bytes(f * float64(unit)), nil
59+
}
60+
i, err := strconv.ParseInt(vs, 10, 64)
61+
if err != nil {
62+
return 0, &ParseErr{
63+
Func: "ParseBytes",
64+
Str: s,
65+
Err: err,
66+
}
67+
}
68+
return Bytes(i * int64(unit)), nil
69+
}

0 commit comments

Comments
 (0)