Skip to content

Commit

Permalink
Update to go 1.22 & fix linting
Browse files Browse the repository at this point in the history
  • Loading branch information
Petya Koleva committed Jun 26, 2024
1 parent 816c36a commit 8b1d64f
Show file tree
Hide file tree
Showing 14 changed files with 122 additions and 94 deletions.
17 changes: 6 additions & 11 deletions conflate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package conflate
import (
gocontext "context"
"net/http"
"os"
"sync"
"testing"
"time"
Expand Down Expand Up @@ -56,14 +55,12 @@ func TestAddData_Expand(t *testing.T) {
c := New()
c.Expand(true)

err := os.Setenv("X", "123")
assert.Nil(t, err)
err = os.Setenv("Y", "str")
assert.Nil(t, err)
t.Setenv("X", "123")
t.Setenv("Y", "str")

inJSON := []byte(`{ "x": $X, "y": "$Y", "z": "$Z"}`)

err = c.AddData(inJSON)
err := c.AddData(inJSON)
assert.Nil(t, err)
outJSON, err := c.MarshalJSON()
assert.Nil(t, err)
Expand All @@ -79,14 +76,12 @@ func TestAddData_NoExpand(t *testing.T) {
c := New()
c.Expand(false)

err := os.Setenv("X", "123")
assert.Nil(t, err)
err = os.Setenv("Y", "str")
assert.Nil(t, err)
t.Setenv("X", "123")
t.Setenv("Y", "str")

inJSON := []byte(`{ "x": "$X" }`)

err = c.AddData(inJSON)
err := c.AddData(inJSON)
assert.Nil(t, err)

outJSON, err := c.MarshalJSON()
Expand Down
4 changes: 2 additions & 2 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import (

type context string

type errWithContext struct {
type contextError struct {
msg string
context context
}

func (e errWithContext) Error() string {
func (e contextError) Error() string {
return fmt.Sprintf("%v (%v)", e.msg, e.context)
}
2 changes: 1 addition & 1 deletion filedata.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func recursiveExpand(b []byte) []byte {

var c int

for i := 0; i < maxExpansions; i++ {
for range maxExpansions {
b, c = expand(b)
if c == 0 {
return b
Expand Down
33 changes: 8 additions & 25 deletions filedata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package conflate
import (
"errors"
pkgurl "net/url"
"os"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -12,13 +11,17 @@ import (
var errTest = errors.New("my error")

func testFiledataNew(t *testing.T, data []byte, path string) (filedata, error) {
t.Helper()

url, err := pkgurl.Parse(path)
assert.Nil(t, err)

return newFiledata(data, url)
}

func testFiledataNewAssert(t *testing.T, data []byte, path string) filedata {
t.Helper()

fd, err := testFiledataNew(t, data, path)
assert.Nil(t, err)

Expand Down Expand Up @@ -188,30 +191,10 @@ func TestFiledata_IncludesError(t *testing.T) {
}

func TestFiledata_Expand(t *testing.T) {
w := os.Getenv("W")
x := os.Getenv("X")
y := os.Getenv("Y")
z := os.Getenv("Z")

err := os.Setenv("W", "$W")
assert.Nil(t, err)
err = os.Setenv("X", `"x"`)
assert.Nil(t, err)
err = os.Setenv("Y", `y`)
assert.Nil(t, err)
err = os.Setenv("Z", `$Y`)
assert.Nil(t, err)

defer func() {
err = os.Setenv("W", w)
assert.Nil(t, err)
err = os.Setenv("X", x)
assert.Nil(t, err)
err = os.Setenv("Y", y)
assert.Nil(t, err)
err = os.Setenv("Z", z)
assert.Nil(t, err)
}()
t.Setenv("W", "$W")
t.Setenv("X", `"x"`)
t.Setenv("Y", `y`)
t.Setenv("Z", `$Y`)

b := recursiveExpand([]byte(`{"W":"$W","X":$X,"Y":"$Y","Z":"$Z"}`))
assert.Equal(t, string(b), string(`{"W":"$W","X":"x","Y":"y","Z":"y"}`))
Expand Down
2 changes: 1 addition & 1 deletion format.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func (f xmlTemplateFormatChecker) IsFormat(input interface{}) bool {

if s, ok := input.(string); ok {
s = f.tags.ReplaceAllString(s, "")
if len(s) > 0 {
if s != "" {
var v interface{}
if err := xml.Unmarshal([]byte(s), &v); err != nil {
ferr = fmt.Errorf("failed to parse xml: %w", err)
Expand Down
6 changes: 6 additions & 0 deletions format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,8 @@ func TestHtmlFormatCheckerIsFormat_NotValid(t *testing.T) {
// --------

func testCryptoFormatCheckerIsFormatNotString(t *testing.T, cryptoType cryptoType) {
t.Helper()

givenName := cryptoName
givenValue := 1

Expand Down Expand Up @@ -243,6 +245,8 @@ func TestCryptoFormatCheckerIsFormat_NotString(t *testing.T) {
}

func testCryptoFormatCheckerIsFormatNotValid(t *testing.T, cryptoType cryptoType) {
t.Helper()

givenName := cryptoName
givenValue := "dGhpcyBpcyBub3QgYSB2YWxpZCBjZXJ0aWZpY2F0ZQo="

Expand Down Expand Up @@ -270,6 +274,8 @@ func TestCryptoFormatCheckerIsFormat_NotValid(t *testing.T) {
}

func testCryptoFormatCheckerIsFormatValid(t *testing.T, cryptoType cryptoType, givenValue string) {
t.Helper()

givenName := cryptoName

formatErrs.clear()
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/miracl/conflate

go 1.21
go 1.22

require (
cloud.google.com/go/storage v1.33.0
Expand Down
8 changes: 3 additions & 5 deletions loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,6 @@ func (l *loader) loadDataRecursive(parentUrls []*pkgurl.URL, data ...filedata) (
var allData filedatas

for _, datum := range data {
datum := datum

childData, err := l.loadDatumRecursive(parentUrls, nil, &datum)
if err != nil {
return nil, err
Expand Down Expand Up @@ -150,7 +148,7 @@ func loadURL(url *pkgurl.URL) ([]byte, error) {

client := http.Client{Transport: newTransport()}

resp, err := client.Get(url.String())
resp, err := client.Get(url.String()) //nolint:noctx // we don't have ctx anywhere
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -301,7 +299,7 @@ func workingDir() (*pkgurl.URL, error) {
func setPath(path string) string {
if goos == windowsOS {
// https://blogs.msdn.microsoft.com/ie/2006/12/06/file-uris-in-windows/
path = strings.Replace(path, `\`, `/`, -1)
path = strings.ReplaceAll(path, `\`, `/`)
path = strings.TrimLeft(path, `/`)

if driveLetter.MatchString(path) {
Expand All @@ -321,7 +319,7 @@ func getPath(path string) string {
path = `//` + path
}

path = strings.Replace(path, `/`, `\`, -1)
path = strings.ReplaceAll(path, `/`, `\`)
}

return path
Expand Down
8 changes: 6 additions & 2 deletions loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,11 @@ func testServer() func() {
}

func testWaitForURL(t *testing.T, urlPath string) {
t.Helper()

// wait for a couple of seconds for server to come up
for i := 0; i < 4; i++ {
resp, err := http.Get(urlPath) //nolint:gosec // ok for a test
for range 4 {
resp, err := http.Get(urlPath) //nolint:gosec,noctx // ok for a test
if err == nil {
//nolint:gocritic // ok for a test with small loop
defer func() {
Expand Down Expand Up @@ -382,6 +384,8 @@ func TestLoadURLsRecursive_BlankChildToml(t *testing.T) {
}

func testPath(t *testing.T, urlPath, filePath string) {
t.Helper()

assert.Equal(t, urlPath, setPath(filePath))
assert.Equal(t, filePath, getPath(urlPath))
}
Expand Down
2 changes: 1 addition & 1 deletion marshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func TestJSONMarshalAll(t *testing.T) {
}

func TestJSONMarshalAll_Error(t *testing.T) {
mockMarshal := func(obj interface{}) ([]byte, error) {
mockMarshal := func(_ interface{}) ([]byte, error) {
return nil, errTest
}
data, err := jsonMarshalAll(mockMarshal, "a")
Expand Down
24 changes: 12 additions & 12 deletions merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ func merge(pToData, fromData interface{}) error {

func mergeRecursive(ctx context, pToData, fromData interface{}) error {
if pToData == nil {
return &errWithContext{
return &contextError{
context: ctx,
msg: "the destination variable must not be nil",
}
}

pToVal := reflect.ValueOf(pToData)
if pToVal.Kind() != reflect.Ptr {
return &errWithContext{
return &contextError{
context: ctx,
msg: "the destination variable must be a pointer",
}
Expand Down Expand Up @@ -69,15 +69,15 @@ func mergeRecursive(ctx context, pToData, fromData interface{}) error {
func mergeMapRecursive(ctx context, toData, fromData interface{}) error {
fromProps, ok := fromData.(map[string]interface{})
if !ok {
return &errWithContext{
return &contextError{
context: ctx,
msg: "the source value must be a map[string]interface{}",
}
}

toProps, _ := toData.(map[string]interface{})
if toProps == nil {
return &errWithContext{
toProps, ok := toData.(map[string]interface{})
if toProps == nil || !ok {
return &contextError{
context: ctx,
msg: "the destination value must be a map[string]interface{}",
}
Expand All @@ -89,7 +89,7 @@ func mergeMapRecursive(ctx context, toData, fromData interface{}) error {
} else {
err := merge(&val, fromProp)
if err != nil {
return &errWithContext{
return &contextError{
context: ctx.add(name),
msg: fmt.Sprintf("failed to merge object property : %v : %v", name, err.Error()),
}
Expand All @@ -105,15 +105,15 @@ func mergeMapRecursive(ctx context, toData, fromData interface{}) error {
func mergeSliceRecursive(ctx context, toVal reflect.Value, toData, fromData interface{}) error {
fromItems, ok := fromData.([]interface{})
if !ok {
return &errWithContext{
return &contextError{
context: ctx,
msg: "the source value must be a []interface{}",
}
}

toItems, _ := toData.([]interface{})
if toItems == nil {
return &errWithContext{
toItems, ok := toData.([]interface{})
if toItems == nil || !ok {
return &contextError{
context: ctx,
msg: "the destination value must be a []interface{}",
}
Expand All @@ -138,7 +138,7 @@ func mergeDefaultRecursive(ctx context, toVal, fromVal reflect.Value, toData, fr
}

if !fromType.AssignableTo(toType) {
return &errWithContext{
return &contextError{
context: ctx,
msg: fmt.Sprintf("the destination type (%v) must be the same as the source type (%v)", toType, fromType),
}
Expand Down
Loading

0 comments on commit 8b1d64f

Please sign in to comment.