Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(lwcomponent): better JSON handling components #946

Merged
merged 1 commit into from
Oct 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 36 additions & 3 deletions lwcomponent/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ var (
type Artifact struct {
OS string `json:"os"`
ARCH string `json:"arch"`
URL string `json:"url"`
URL string `json:"url,omitempty"`
Signature string `json:"signature"`
//Size ?
}
Expand All @@ -255,12 +255,45 @@ type Component struct {
Name string `json:"name"`
Description string `json:"description"`
Type Type `json:"type"`
LatestVersion semver.Version `json:"version"`
LatestVersion semver.Version `json:"-"`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need this field.

Copy link
Collaborator Author

@hazedav hazedav Oct 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please see MarshalJSON() and UnmarshalJSON() methods for Component which properly handles this field via LatestVersionString. Also TestJSON shows this properly being marshaled and unmarshaled.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe:

Suggested change
LatestVersion semver.Version `json:"-"`
LatestVersion semver.Version `json:"version,omitempty"`

Copy link
Collaborator Author

@hazedav hazedav Oct 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please see MarshalJSON() and UnmarshalJSON() methods for Component which properly handles this field via LatestVersionString. Also TestJSON shows this properly being marshaled and unmarshaled.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're way to smart!

Artifacts []Artifact `json:"artifacts"`
Breadcrumbs Breadcrumbs `json:"breadcrumbs,omitempty"`

// @dhazekamp command_name required when CLICommand is true?
CommandName string `json:"command_name"`
CommandName string `json:"command_name,omitempty"`
}

func (c *Component) UnmarshalJSON(data []byte) error {
type ComponentAlias Component
type T struct {
*ComponentAlias `json:",inline"`
LatestVersionString string `json:"version"`
}

temp := &T{ComponentAlias: (*ComponentAlias)(c)}
err := json.Unmarshal(data, temp)
if err != nil {
return err
}

latestVersion, err := semver.NewVersion(temp.LatestVersionString)
if err != nil {
return err
}
c.LatestVersion = *latestVersion

return nil
}

func (c Component) MarshalJSON() ([]byte, error) {
type ComponentAlias Component
type T struct {
ComponentAlias `json:",inline"`
LatestVersionString string `json:"version"`
}

obj := &T{ComponentAlias: (ComponentAlias)(c), LatestVersionString: c.LatestVersion.String()}
return json.Marshal(obj)
}

// RootPath returns the component's root path ("Dir()/{name}")
Expand Down
41 changes: 41 additions & 0 deletions lwcomponent/component_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package lwcomponent_test
import (
_ "embed"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"net/http"
Expand Down Expand Up @@ -402,3 +403,43 @@ func TestRunAndOutput(t *testing.T) {
})
}
}

func TestJSON(t *testing.T) {
mockComponentString := `{
"name": "lacework-mock-component3",
"description": "This is a description",
"type": "CLI_COMMAND",
"artifacts": [
{
"os": "darwin",
"arch": "amd64",
"signature": "abcdef0123456789"
}
],
"breadcrumbs": {
"installationMessage": "Install me",
"updateMessage": "Update me"
},
"version": "0.1.2"
}`

var c lwcomponent.Component
err := json.Unmarshal([]byte(mockComponentString), &c)
if err != nil {
assert.FailNow(t, err.Error())
}

cBytes, err := json.MarshalIndent(c, "", " ")
if err != nil {
assert.FailNow(t, err.Error())
}
fmt.Print(string(cBytes))
assert.Equal(t, mockComponentString, string(cBytes))

cBytes, err = json.MarshalIndent(&c, "", " ")
if err != nil {
assert.FailNow(t, err.Error())
}
fmt.Print(string(cBytes))
assert.Equal(t, mockComponentString, string(cBytes))
}