Skip to content

Commit

Permalink
fix: retain backward compatibility of newline stripping
Browse files Browse the repository at this point in the history
  • Loading branch information
MatthewDolan committed Jul 19, 2023
1 parent 3e21610 commit f755a86
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions lambda/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type handlerOptions struct {
baseContext context.Context
jsonEncoderOptions []func(encoder *json.Encoder)
jsonDecoderOptions []func(decoder *json.Decoder)
setIndentUsed bool
enableSIGTERM bool
sigtermCallbacks []func()
}
Expand Down Expand Up @@ -112,8 +113,14 @@ func WithSetEscapeHTML(escapeHTML bool) Option {
// lambda.WithSetIndent(">"," "),
// )
func WithSetIndent(prefix, indent string) Option {
return WithJSONEncoderOption(func(encoder *json.Encoder) {
encoder.SetIndent(prefix, indent)
return Option(func(h *handlerOptions) {
// back-compat, the encoder's trailing newline is stripped unless WithSetIndent was used
if prefix != "" || indent != "" {
h.setIndentUsed = true
}
h.jsonEncoderOptions = append(h.jsonEncoderOptions, func(encoder *json.Encoder) {
encoder.SetIndent(prefix, indent)
})
})
}

Expand Down Expand Up @@ -362,7 +369,7 @@ func reflectHandler(f interface{}, h *handlerOptions) handlerFunc {
}

// back-compat, strip the encoder's trailing newline unless WithSetIndent was used
if h.jsonResponseIndentValue == "" && h.jsonResponseIndentPrefix == "" {
if !h.setIndentUsed {
out.Truncate(out.Len() - 1)
}
return out, nil
Expand Down

0 comments on commit f755a86

Please sign in to comment.