Skip to content

Commit

Permalink
Merge pull request #355 from fujiwara/fix/v1-diff-url
Browse files Browse the repository at this point in the history
fix diff output of function url.
  • Loading branch information
fujiwara authored Feb 7, 2024
2 parents d304a7b + 46a8f94 commit b3ec4d3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
14 changes: 10 additions & 4 deletions diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,6 @@ func (app *App) Diff(ctx context.Context, opt *DiffOption) error {

func (app *App) diffFunctionURL(ctx context.Context, name string, opt *DiffOption) error {
var remote, local *types.FunctionUrlConfig
fqName := fullQualifiedFunctionName(name, opt.Qualifier)

fu, err := app.loadFunctionUrl(opt.FunctionURL, name)
if err != nil {
Expand All @@ -154,10 +153,17 @@ func (app *App) diffFunctionURL(ctx context.Context, name string, opt *DiffOptio
InvokeMode: fu.Config.InvokeMode,
}
}
var qualifier *string
if opt.Qualifier != nil {
qualifier = opt.Qualifier
} else if fu.Config != nil && fu.Config.Qualifier != nil {
qualifier = fu.Config.Qualifier
}
fqName := fullQualifiedFunctionName(name, qualifier)

if res, err := app.lambda.GetFunctionUrlConfig(ctx, &lambda.GetFunctionUrlConfigInput{
FunctionName: &name,
Qualifier: opt.Qualifier,
Qualifier: qualifier,
}); err != nil {
var nfe *types.ResourceNotFoundException
if errors.As(err, &nfe) {
Expand All @@ -174,8 +180,8 @@ func (app *App) diffFunctionURL(ctx context.Context, name string, opt *DiffOptio
InvokeMode: res.InvokeMode,
}
}
r, _ := marshalJSON(remote)
l, _ := marshalJSON(local)
r, _ := toGeneralMap(remote, true)
l, _ := toGeneralMap(local, true)

if diff, err := jsondiff.Diff(
&jsondiff.Input{Name: fqName, X: r},
Expand Down
17 changes: 14 additions & 3 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,27 @@ func (app *App) saveFile(path string, b []byte, mode os.FileMode) error {
return os.WriteFile(path, b, mode)
}

func marshalJSON(s interface{}) ([]byte, error) {
func toGeneralMap(s any, omitEmpty bool) (any, error) {
b, err := json.Marshal(s)
if err != nil {
return nil, err
}
x := make(map[string]interface{})
x := make(map[string]any)
if err := json.Unmarshal(b, &x); err != nil {
return nil, err
}
if b, err := json.MarshalIndent(omitEmptyValues(x), "", " "); err != nil {
if omitEmpty {
return omitEmptyValues(x), nil
}
return x, nil
}

func marshalJSON(s interface{}) ([]byte, error) {
x, err := toGeneralMap(s, true)
if err != nil {
return nil, err
}
if b, err := json.MarshalIndent(x, "", " "); err != nil {
return nil, err
} else {
return append(b, '\n'), nil
Expand Down

0 comments on commit b3ec4d3

Please sign in to comment.