Support json.RawMessage in configuration env overrides (#23610)

* support json.RawMessage in env overrides
Этот коммит содержится в:
Doug Lauder
2023-06-07 17:21:58 -04:00
коммит произвёл GitHub
родитель 803d0c6e60
Коммит ac3c0da8ff
6 изменённых файлов: 53 добавлений и 48 удалений

Просмотреть файл

@@ -1336,31 +1336,23 @@ func (s *LogSettings) SetDefaults() {
}
if utils.IsEmptyJSON(s.AdvancedLoggingJSON) {
// copy any non-empty AdvancedLoggingConfig (deprecated) to the new field.
if s.AdvancedLoggingConfig != nil && !utils.IsEmptyJSON([]byte(*s.AdvancedLoggingConfig)) {
s.AdvancedLoggingJSON = utils.StringPtrToJSON(s.AdvancedLoggingConfig)
} else {
s.AdvancedLoggingJSON = []byte("{}")
}
s.AdvancedLoggingJSON = []byte("{}")
}
// temporarily let AdvancedLoggingConfig take precedence.
if s.AdvancedLoggingConfig == nil {
s.AdvancedLoggingConfig = NewString("")
}
//s.AdvancedLoggingConfig = nil
}
// GetAdvancedLoggingConfig returns the advanced logging config as a []byte.
// AdvancedLoggingJSON takes precident over the deprecated AdvancedLoggingConfig.
// AdvancedLoggingJSON takes precedence over the deprecated AdvancedLoggingConfig.
func (s *LogSettings) GetAdvancedLoggingConfig() []byte {
// temporarily let AdvancedLoggingConfig take precedence.
if s.AdvancedLoggingConfig != nil && !utils.IsEmptyJSON([]byte(*s.AdvancedLoggingConfig)) {
return []byte(*s.AdvancedLoggingConfig)
}
if !utils.IsEmptyJSON(s.AdvancedLoggingJSON) {
return s.AdvancedLoggingJSON
}
if s.AdvancedLoggingConfig != nil && !utils.IsEmptyJSON([]byte(*s.AdvancedLoggingConfig)) {
return []byte(*s.AdvancedLoggingConfig)
}
return []byte("{}")
}
@@ -1406,31 +1398,23 @@ func (s *ExperimentalAuditSettings) SetDefaults() {
}
if utils.IsEmptyJSON(s.AdvancedLoggingJSON) {
// copy any non-empty AdvancedLoggingConfig (deprecated) to the new field.
if s.AdvancedLoggingConfig != nil && !utils.IsEmptyJSON([]byte(*s.AdvancedLoggingConfig)) {
s.AdvancedLoggingJSON = utils.StringPtrToJSON(s.AdvancedLoggingConfig)
} else {
s.AdvancedLoggingJSON = []byte("{}")
}
s.AdvancedLoggingJSON = []byte("{}")
}
// temporarily let AdvancedLoggingConfig take precedence.
if s.AdvancedLoggingConfig == nil {
s.AdvancedLoggingConfig = NewString("")
}
//s.AdvancedLoggingConfig = nil
}
// GetAdvancedLoggingConfig returns the advanced logging config as a []byte.
// AdvancedLoggingJSON takes precident over the deprecated AdvancedLoggingConfig.
// AdvancedLoggingJSON takes precedence over the deprecated AdvancedLoggingConfig.
func (s *ExperimentalAuditSettings) GetAdvancedLoggingConfig() []byte {
// temporarily let AdvancedLoggingConfig take precedence.
if s.AdvancedLoggingConfig != nil && !utils.IsEmptyJSON([]byte(*s.AdvancedLoggingConfig)) {
return []byte(*s.AdvancedLoggingConfig)
}
if !utils.IsEmptyJSON(s.AdvancedLoggingJSON) {
return s.AdvancedLoggingJSON
}
if s.AdvancedLoggingConfig != nil && !utils.IsEmptyJSON([]byte(*s.AdvancedLoggingConfig)) {
return []byte(*s.AdvancedLoggingConfig)
}
return []byte("{}")
}
@@ -1481,30 +1465,23 @@ func (s *NotificationLogSettings) SetDefaults() {
}
if utils.IsEmptyJSON(s.AdvancedLoggingJSON) {
// copy any non-empty AdvancedLoggingConfig (deprecated) to the new field.
if s.AdvancedLoggingConfig != nil && !utils.IsEmptyJSON([]byte(*s.AdvancedLoggingConfig)) {
s.AdvancedLoggingJSON = utils.StringPtrToJSON(s.AdvancedLoggingConfig)
} else {
s.AdvancedLoggingJSON = []byte("{}")
}
s.AdvancedLoggingJSON = []byte("{}")
}
// temporarily let AdvancedLoggingConfig take precedence.
if s.AdvancedLoggingConfig == nil {
s.AdvancedLoggingConfig = NewString("")
}
//s.AdvancedLoggingConfig = nil
}
// GetAdvancedLoggingConfig returns the advanced logging config as a []byte.
// AdvancedLoggingJSON takes precident over the deprecated AdvancedLoggingConfig.
// AdvancedLoggingJSON takes precedence over the deprecated AdvancedLoggingConfig.
func (s *NotificationLogSettings) GetAdvancedLoggingConfig() []byte {
// temporarily let AdvancedLoggingConfig take precedence.
if s.AdvancedLoggingConfig != nil && !utils.IsEmptyJSON([]byte(*s.AdvancedLoggingConfig)) {
return []byte(*s.AdvancedLoggingConfig)
}
if !utils.IsEmptyJSON(s.AdvancedLoggingJSON) {
return s.AdvancedLoggingJSON
}
if s.AdvancedLoggingConfig != nil && !utils.IsEmptyJSON([]byte(*s.AdvancedLoggingConfig)) {
return []byte(*s.AdvancedLoggingConfig)
}
return []byte("{}")
}

Просмотреть файл

@@ -7,6 +7,7 @@ import (
"bytes"
"encoding/json"
"strings"
"unicode"
"github.com/pkg/errors"
)
@@ -57,7 +58,19 @@ func NewHumanizedJSONError(err error, data []byte, offset int64) *HumanizedJSONE
}
func IsEmptyJSON(j json.RawMessage) bool {
if len(j) == 0 || bytes.Equal(j, []byte("{}")) || bytes.Equal(j, []byte("\"\"")) || bytes.Equal(j, []byte("[]")) {
if len(j) == 0 {
return true
}
// remove all whitespace
jj := make([]byte, 0, len(j))
for _, b := range j {
if !unicode.IsSpace(rune(b)) {
jj = append(jj, b)
}
}
if len(jj) == 0 || bytes.Equal(jj, []byte("{}")) || bytes.Equal(jj, []byte("\"\"")) || bytes.Equal(jj, []byte("[]")) {
return true
}
return false

Просмотреть файл

@@ -283,6 +283,11 @@ func TestIsJSONEmpty(t *testing.T) {
[]byte("\"hello\""),
false,
},
{
"whitespace still empty",
[]byte(" \n { \t } "),
true,
},
}
for _, testCase := range testCases {