Skip to content

Commit

Permalink
fix(lwcomponent): better JSON handling components (#946)
Browse files Browse the repository at this point in the history
  • Loading branch information
hazedav committed Oct 14, 2022
1 parent c8ead2c commit 8edb92d
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 3 deletions.
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:"-"`
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))
}

0 comments on commit 8edb92d

Please sign in to comment.