Skip to content

Commit

Permalink
Move template funcs from utils package to template package
Browse files Browse the repository at this point in the history
  • Loading branch information
bporter816 committed Mar 30, 2024
1 parent 422092f commit 9f17301
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 93 deletions.
31 changes: 31 additions & 0 deletions template/funcs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package template

import (
"encoding/hex"
)

func FormatSerial(bytes []byte) string {
if len(bytes) == 0 {
return ""
}
dst := make([]byte, hex.EncodedLen(len(bytes)))
hex.Encode(dst, bytes)
final := make([]byte, 3*len(bytes))
for i := 0; i < len(bytes); i++ {
final[3*i] = dst[2*i]
final[3*i+1] = dst[2*i+1]
final[3*i+2] = ':'
}
return string(final[:len(final)-1])
}

func Chunk(str string, length int) []string {
chunks := make([]string, 0)
for i := 0; i <= len(str)-length; i += length {
chunks = append(chunks, str[i:i+length])
}
if len(str)%length != 0 {
chunks = append(chunks, str[len(str)-len(str)%length:])
}
return chunks
}
69 changes: 69 additions & 0 deletions template/funcs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package template

import (
"reflect"
"testing"
)

func TestFormatSerial(t *testing.T) {
tests := []struct {
input []byte
expected string
}{
{
input: []byte{},
expected: "",
},
{
input: []byte{0x00},
expected: "00",
},
{
input: []byte{0x12, 0x34},
expected: "12:34",
},
}

for _, tc := range tests {
got := FormatSerial(tc.input)
if got != tc.expected {
t.Fatalf("expected: %v, got: %v", tc.expected, got)
}
}
}

func TestChunk(t *testing.T) {
tests := []struct {
str string
length int
expected []string
}{
{
str: "",
length: 2,
expected: []string{},
},
{
str: "aabbccdd",
length: 2,
expected: []string{"aa", "bb", "cc", "dd"},
},
{
str: "aabbccd",
length: 2,
expected: []string{"aa", "bb", "cc", "d"},
},
{
str: "aaa",
length: 5,
expected: []string{"aaa"},
},
}

for _, tc := range tests {
got := Chunk(tc.str, tc.length)
if !reflect.DeepEqual(got, tc.expected) {
t.Fatalf("expected: %v, got: %v", tc.expected, got)
}
}
}
4 changes: 2 additions & 2 deletions template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ var (
}
return b.Bytes()
},
"chunk": func(str string, length int) []string { return utils.Chunk(str, length) },
"formatSerial": func(bytes []byte) string { return utils.FormatSerial(bytes) },
"chunk": func(str string, length int) []string { return Chunk(str, length) },
"formatSerial": func(bytes []byte) string { return FormatSerial(bytes) },
"formatTime": func(t time.Time) string { return t.Format(utils.DefaultTimeFormat) },
"isRSA": func(algo x509.PublicKeyAlgorithm) bool { return algo == x509.RSA },
"join": func(arr []string, sep string) string { return strings.Join(arr, sep) },
Expand Down
27 changes: 0 additions & 27 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package utils

import (
"crypto/x509"
"encoding/hex"
"encoding/pem"
"github.com/aws/aws-sdk-go-v2/aws/arn"
"golang.org/x/text/cases"
Expand Down Expand Up @@ -133,32 +132,6 @@ func ParseCertsFromPEM(data []byte) ([]*x509.Certificate, error) {
return x509.ParseCertificates(bytes)
}

func FormatSerial(bytes []byte) string {
if len(bytes) == 0 {
return ""
}
dst := make([]byte, hex.EncodedLen(len(bytes)))
hex.Encode(dst, bytes)
final := make([]byte, 3*len(bytes))
for i := 0; i < len(bytes); i++ {
final[3*i] = dst[2*i]
final[3*i+1] = dst[2*i+1]
final[3*i+2] = ':'
}
return string(final[:len(final)-1])
}

func Chunk(str string, length int) []string {
chunks := make([]string, 0)
for i := 0; i <= len(str)-length; i += length {
chunks = append(chunks, str[i:i+length])
}
if len(str)%length != 0 {
chunks = append(chunks, str[len(str)-len(str)%length:])
}
return chunks
}

func FormatSize(size int64, precision int) string {
// keep bytes as integers
if size < 1024 {
Expand Down
64 changes: 0 additions & 64 deletions utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package utils

import (
"github.com/aws/aws-sdk-go-v2/aws/arn"
"reflect"
"testing"
)

Expand Down Expand Up @@ -133,69 +132,6 @@ func TestGetResourceNameFromArn(t *testing.T) {
}
}

func TestFormatSerial(t *testing.T) {
tests := []struct {
input []byte
expected string
}{
{
input: []byte{},
expected: "",
},
{
input: []byte{0x00},
expected: "00",
},
{
input: []byte{0x12, 0x34},
expected: "12:34",
},
}

for _, tc := range tests {
got := FormatSerial(tc.input)
if got != tc.expected {
t.Fatalf("expected: %v, got: %v", tc.expected, got)
}
}
}

func TestChunk(t *testing.T) {
tests := []struct {
str string
length int
expected []string
}{
{
str: "",
length: 2,
expected: []string{},
},
{
str: "aabbccdd",
length: 2,
expected: []string{"aa", "bb", "cc", "dd"},
},
{
str: "aabbccd",
length: 2,
expected: []string{"aa", "bb", "cc", "d"},
},
{
str: "aaa",
length: 5,
expected: []string{"aaa"},
},
}

for _, tc := range tests {
got := Chunk(tc.str, tc.length)
if !reflect.DeepEqual(got, tc.expected) {
t.Fatalf("expected: %v, got: %v", tc.expected, got)
}
}
}

func TestFormatSize(t *testing.T) {
tests := []struct {
size int64
Expand Down

0 comments on commit 9f17301

Please sign in to comment.