Skip to content

Commit

Permalink
fix: some lint problems
Browse files Browse the repository at this point in the history
  • Loading branch information
elliotxx committed Sep 13, 2023
1 parent 5475913 commit 8a44af4
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 42 deletions.
2 changes: 1 addition & 1 deletion .golangci.yml
Expand Up @@ -25,7 +25,7 @@ linters:
- bodyclose
- depguard
- dogsled
- dupl
# - dupl
- exportloopref
- exhaustive
- goconst
Expand Down
2 changes: 1 addition & 1 deletion core.go
Expand Up @@ -63,7 +63,7 @@ func GetLegacyConditionsFn(u *unstructured.Unstructured) GetConditionsFn {
}

// AlwaysReady Used for resources that are always ready
func AlwaysReady(u *unstructured.Unstructured) (*Result, error) {
func AlwaysReady(_ *unstructured.Unstructured) (*Result, error) {
return &Result{
Status: CurrentStatus,
Message: "Resource is always ready",
Expand Down
37 changes: 0 additions & 37 deletions example_test.go
@@ -1,13 +1,10 @@
package kstatus

import (
"encoding/json"
"fmt"
"strings"
"testing"

"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v2"
)

func TestExampleCompute(t *testing.T) {
Expand Down Expand Up @@ -92,37 +89,3 @@ status:

assert.Equal(t, expectedManifest, receivedManifest)
}

// marshal marshals the object into JSON then converts JSON to YAML and returns the
// YAML.
func marshal(o interface{}) ([]byte, error) {
j, err := json.Marshal(o)
if err != nil {
return nil, fmt.Errorf("error marshaling into JSON: %v", err)
}

y, err := json2yaml(j)
if err != nil {
return nil, fmt.Errorf("error converting JSON to YAML: %v", err)
}

return y, nil
}

// json2yaml Converts JSON to YAML.
func json2yaml(j []byte) ([]byte, error) {
// Convert the JSON to an object.
var jsonObj interface{}
// We are using yaml.Unmarshal here (instead of json.Unmarshal) because the
// Go JSON library doesn't try to pick the right number type (int, float,
// etc.) when unmarshalling to interface{}, it just picks float64
// universally. go-yaml does go through the effort of picking the right
// number type, so we can preserve number type throughout this process.
err := yaml.Unmarshal(j, &jsonObj)
if err != nil {
return nil, err
}

// Marshal this object into YAML.
return yaml.Marshal(jsonObj)
}
1 change: 1 addition & 0 deletions status_augment_test.go
Expand Up @@ -32,6 +32,7 @@ metadata:
var timestamp = time.Now().Add(-1 * time.Minute).UTC().Format(time.RFC3339)

func addConditions(t *testing.T, u *unstructured.Unstructured, conditions []map[string]interface{}) {
t.Helper()
conds := make([]interface{}, 0)
for _, c := range conditions {
conds = append(conds, c)
Expand Down
5 changes: 4 additions & 1 deletion status_compute_test.go
Expand Up @@ -8,10 +8,12 @@ import (
"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"sigs.k8s.io/yaml"
)

func y2u(t *testing.T, spec string) *unstructured.Unstructured {
j, err := yaml2json([]byte(spec))
t.Helper()
j, err := yaml.YAMLToJSON([]byte(spec))
assert.NoError(t, err)
u, _, err := unstructured.UnstructuredJSONScheme.Decode(j, nil, nil)
assert.NoError(t, err)
Expand All @@ -26,6 +28,7 @@ type testSpec struct {
}

func runStatusTest(t *testing.T, tc testSpec) {
t.Helper()
res, err := Compute(y2u(t, tc.spec))
assert.NoError(t, err)
assert.Equal(t, tc.expectedStatus, res.Status)
Expand Down
42 changes: 40 additions & 2 deletions testing.go
@@ -1,18 +1,56 @@
package kstatus

import (
"encoding/json"
"fmt"
"testing"

"gopkg.in/yaml.v2"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/util/yaml"
k8syaml "k8s.io/apimachinery/pkg/util/yaml"
)

func YamlToUnstructured(t *testing.T, yml string) *unstructured.Unstructured {
t.Helper()
m := make(map[string]interface{})
err := yaml.Unmarshal([]byte(yml), &m)
err := k8syaml.Unmarshal([]byte(yml), &m)
if err != nil {
t.Fatalf("error parsing yaml: %v", err)
return nil
}
return &unstructured.Unstructured{Object: m}
}

// marshal marshals the object into JSON then converts JSON to YAML and returns the
// YAML.
func marshal(o interface{}) ([]byte, error) {
j, err := json.Marshal(o)
if err != nil {
return nil, fmt.Errorf("error marshaling into JSON: %v", err)
}

y, err := json2yaml(j)
if err != nil {
return nil, fmt.Errorf("error converting JSON to YAML: %v", err)
}

return y, nil
}

// json2yaml Converts JSON to YAML.
func json2yaml(j []byte) ([]byte, error) {
// Convert the JSON to an object.
var jsonObj interface{}
// We are using yaml.Unmarshal here (instead of json.Unmarshal) because the
// Go JSON library doesn't try to pick the right number type (int, float,
// etc.) when unmarshalling to interface{}, it just picks float64
// universally. go-yaml does go through the effort of picking the right
// number type, so we can preserve number type throughout this process.
err := yaml.Unmarshal(j, &jsonObj)
if err != nil {
return nil, err
}

// Marshal this object into YAML.
return yaml.Marshal(jsonObj)
}

0 comments on commit 8a44af4

Please sign in to comment.