Skip to content

Commit

Permalink
omit jsonutil
Browse files Browse the repository at this point in the history
  • Loading branch information
fujiwara committed Jul 14, 2023
1 parent 1c9628e commit 1f7b4e3
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 17 deletions.
50 changes: 50 additions & 0 deletions json.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package lambroll

func isEmptyValue(value interface{}) bool {
switch v := value.(type) {
case nil:
return true
case string:
return v == ""
case bool:
return !v
case map[string]interface{}:
return len(v) == 0
case []interface{}:
return len(v) == 0
default:
return false
}
}

func omitEmptyValues(data interface{}) interface{} {
switch v := data.(type) {
case map[string]interface{}:
nonEmptyMap := make(map[string]interface{})
for key, value := range v {
nonEmptyValue := omitEmptyValues(value)
if !isEmptyValue(nonEmptyValue) {
nonEmptyMap[key] = nonEmptyValue
}
}
if len(nonEmptyMap) != 0 {
return nonEmptyMap
}
case []interface{}:
nonEmptyList := make([]interface{}, 0)
for _, value := range v {
nonEmptyValue := omitEmptyValues(value)
if !isEmptyValue(nonEmptyValue) {
nonEmptyList = append(nonEmptyList, nonEmptyValue)
}
}
if len(nonEmptyList) != 0 {
return nonEmptyList
}
default:
if !isEmptyValue(v) {
return v
}
}
return nil
}
15 changes: 9 additions & 6 deletions list.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package lambroll

import (
"context"
"fmt"
"log"
"os"

lambdav2 "github.com/aws/aws-sdk-go-v2/service/lambda"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/lambda"
"github.com/pkg/errors"
)

Expand All @@ -15,24 +17,25 @@ type ListOption struct {

// List lists lambda functions
func (app *App) List(opt ListOption) error {
ctx := context.TODO()
var marker *string
for {
res, err := app.lambda.ListFunctions(&lambda.ListFunctionsInput{
MaxItems: aws.Int64(50),
res, err := app.lambdav2.ListFunctions(ctx, &lambdav2.ListFunctionsInput{
MaxItems: aws.Int32(50),
})
if err != nil {
return errors.Wrap(err, "failed to ListFunctions")
return fmt.Errorf("failed to ListFunctions: %w", err)
}
for _, c := range res.Functions {
arn := app.functionArn(*c.FunctionName)
log.Printf("[debug] listing tags of %s", arn)
res, err := app.lambda.ListTags(&lambda.ListTagsInput{
res, err := app.lambdav2.ListTags(ctx, &lambdav2.ListTagsInput{
Resource: aws.String(arn),
})
if err != nil {
return errors.Wrap(err, "faled to list tags")
}
b, _ := marshalJSON(newFunctionFrom(c, nil, res.Tags))
b, _ := marshalJSONV2(newFunctionFromV2(&c, nil, res.Tags))
os.Stdout.Write(b)
}
if marker = res.NextMarker; marker == nil {
Expand Down
22 changes: 11 additions & 11 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"strings"

"github.com/Songmu/prompter"
"github.com/aws/aws-sdk-go/private/protocol/json/jsonutil"
)

func (app *App) saveFile(path string, b []byte, mode os.FileMode) error {
Expand All @@ -23,19 +22,20 @@ func (app *App) saveFile(path string, b []byte, mode os.FileMode) error {
return ioutil.WriteFile(path, b, mode)
}

func marshalJSON(s interface{}) ([]byte, error) {
var buf bytes.Buffer
b, err := jsonutil.BuildJSON(s)
func marshalJSONV2(s interface{}) ([]byte, error) {
b, err := json.Marshal(s)
if err != nil {
return nil, err
}
json.Indent(&buf, b, "", " ")
buf.WriteString("\n")
return buf.Bytes(), nil
}

func marshalJSONV2(s interface{}) ([]byte, error) {
return json.MarshalIndent(s, "", " ")
x := make(map[string]interface{})
if err := json.Unmarshal(b, &x); err != nil {
return nil, err
}
if b, err := json.MarshalIndent(omitEmptyValues(x), "", " "); err != nil {
return nil, err
} else {
return append(b, '\n'), nil
}
}

func unmarshalJSON(src []byte, v interface{}, path string) error {
Expand Down

0 comments on commit 1f7b4e3

Please sign in to comment.