Chore: use any rather than interface{} (#74066)

This commit is contained in:
Ryan McKinley
2023-08-30 18:46:47 +03:00
committed by GitHub
parent 3e272d2bda
commit 025b2f3011
525 changed files with 2528 additions and 2528 deletions
+2 -2
View File
@@ -95,7 +95,7 @@ func CalculateDiff(ctx context.Context, options *Options, baseData, newData *sim
}
// getDiff computes the diff of two dashboard versions.
func getDiff(baseData, newData *simplejson.Json) (interface{}, diff.Diff, error) {
func getDiff(baseData, newData *simplejson.Json) (any, diff.Diff, error) {
leftBytes, err := baseData.Encode()
if err != nil {
return nil, nil, err
@@ -115,7 +115,7 @@ func getDiff(baseData, newData *simplejson.Json) (interface{}, diff.Diff, error)
return nil, nil, ErrNilDiff
}
left := make(map[string]interface{})
left := make(map[string]any)
err = json.Unmarshal(leftBytes, &left)
if err != nil {
return nil, nil, err
+5 -5
View File
@@ -22,8 +22,8 @@ type BasicDiff struct {
// A BasicBlock represents a top-level element in a basic diff.
type BasicBlock struct {
Title string
Old interface{}
New interface{}
Old any
New any
Change ChangeType
Changes []*BasicChange
Summaries []*BasicSummary
@@ -35,8 +35,8 @@ type BasicBlock struct {
// BasicChanges in a BasicBlock.
type BasicChange struct {
Key string
Old interface{}
New interface{}
Old any
New any
Change ChangeType
LineStart int
LineEnd int
@@ -60,7 +60,7 @@ type BasicFormatter struct {
tpl *template.Template
}
func NewBasicFormatter(left interface{}) *BasicFormatter {
func NewBasicFormatter(left any) *BasicFormatter {
tpl := template.Must(template.New("block").Funcs(tplFuncMap).Parse(tplBlock))
tpl = template.Must(tpl.New("change").Funcs(tplFuncMap).Parse(tplChange))
tpl = template.Must(tpl.New("summary").Funcs(tplFuncMap).Parse(tplSummary))
+28 -28
View File
@@ -86,17 +86,17 @@ var diffTplFuncs = template.FuncMap{
// and contains the data required to produce the tokens output in the basic
// diff.
type JSONLine struct {
LineNum int `json:"line"`
LeftLine int `json:"leftLine"`
RightLine int `json:"rightLine"`
Indent int `json:"indent"`
Text string `json:"text"`
Change ChangeType `json:"changeType"`
Key string `json:"key"`
Val interface{} `json:"value"`
LineNum int `json:"line"`
LeftLine int `json:"leftLine"`
RightLine int `json:"rightLine"`
Indent int `json:"indent"`
Text string `json:"text"`
Change ChangeType `json:"changeType"`
Key string `json:"key"`
Val any `json:"value"`
}
func NewJSONFormatter(left interface{}) *JSONFormatter {
func NewJSONFormatter(left any) *JSONFormatter {
tpl := template.Must(template.New("JSONDiffWrapper").Funcs(diffTplFuncs).Parse(tplJSONDiffWrapper))
tpl = template.Must(tpl.New("JSONDiffLine").Funcs(diffTplFuncs).Parse(tplJSONDiffLine))
@@ -112,7 +112,7 @@ func NewJSONFormatter(left interface{}) *JSONFormatter {
}
type JSONFormatter struct {
left interface{}
left any
path []string
size []int
inArray []bool
@@ -130,7 +130,7 @@ type AsciiLine struct {
// the actual changes - no formatting
key string
val interface{}
val any
// level of indentation for the current line
indent int
@@ -140,16 +140,16 @@ type AsciiLine struct {
}
func (f *JSONFormatter) Format(diff diff.Diff) (result string, err error) {
if v, ok := f.left.(map[string]interface{}); ok {
if v, ok := f.left.(map[string]any); ok {
if err := f.formatObject(v, diff); err != nil {
return "", err
}
} else if v, ok := f.left.([]interface{}); ok {
} else if v, ok := f.left.([]any); ok {
if err := f.formatArray(v, diff); err != nil {
return "", err
}
} else {
return "", fmt.Errorf("expected map[string]interface{} or []interface{}, got %T",
return "", fmt.Errorf("expected map[string]any or []any, got %T",
f.left)
}
@@ -163,7 +163,7 @@ func (f *JSONFormatter) Format(diff diff.Diff) (result string, err error) {
return b.String(), nil
}
func (f *JSONFormatter) formatObject(left map[string]interface{}, df diff.Diff) error {
func (f *JSONFormatter) formatObject(left map[string]any, df diff.Diff) error {
f.addLineWith(ChangeNil, "{")
f.push("ROOT", len(left), false)
if err := f.processObject(left, df.Deltas()); err != nil {
@@ -176,7 +176,7 @@ func (f *JSONFormatter) formatObject(left map[string]interface{}, df diff.Diff)
return nil
}
func (f *JSONFormatter) formatArray(left []interface{}, df diff.Diff) error {
func (f *JSONFormatter) formatArray(left []any, df diff.Diff) error {
f.addLineWith(ChangeNil, "[")
f.push("ROOT", len(left), true)
if err := f.processArray(left, df.Deltas()); err != nil {
@@ -189,7 +189,7 @@ func (f *JSONFormatter) formatArray(left []interface{}, df diff.Diff) error {
return nil
}
func (f *JSONFormatter) processArray(array []interface{}, deltas []diff.Delta) error {
func (f *JSONFormatter) processArray(array []any, deltas []diff.Delta) error {
patchedIndex := 0
for index, value := range array {
if err := f.processItem(value, deltas, diff.Index(index)); err != nil {
@@ -214,7 +214,7 @@ func (f *JSONFormatter) processArray(array []interface{}, deltas []diff.Delta) e
return nil
}
func (f *JSONFormatter) processObject(object map[string]interface{}, deltas []diff.Delta) error {
func (f *JSONFormatter) processObject(object map[string]any, deltas []diff.Delta) error {
names := sortKeys(object)
for _, name := range names {
value := object[name]
@@ -234,7 +234,7 @@ func (f *JSONFormatter) processObject(object map[string]interface{}, deltas []di
return nil
}
func (f *JSONFormatter) processItem(value interface{}, deltas []diff.Delta, position diff.Position) error {
func (f *JSONFormatter) processItem(value any, deltas []diff.Delta, position diff.Position) error {
matchedDeltas := f.searchDeltas(deltas, position)
positionStr := position.String()
if len(matchedDeltas) > 0 {
@@ -242,12 +242,12 @@ func (f *JSONFormatter) processItem(value interface{}, deltas []diff.Delta, posi
switch matchedDelta := matchedDelta.(type) {
case *diff.Object:
switch value.(type) {
case map[string]interface{}:
case map[string]any:
// ok
default:
return errors.New("type mismatch")
}
o := value.(map[string]interface{})
o := value.(map[string]any)
f.newLine(ChangeNil)
f.printKey(positionStr)
@@ -267,12 +267,12 @@ func (f *JSONFormatter) processItem(value interface{}, deltas []diff.Delta, posi
case *diff.Array:
switch value.(type) {
case []interface{}:
case []any:
// ok
default:
return errors.New("type mismatch")
}
a := value.([]interface{})
a := value.([]any)
f.newLine(ChangeNil)
f.printKey(positionStr)
@@ -416,7 +416,7 @@ func (f *JSONFormatter) printComma() {
}
}
func (f *JSONFormatter) printValue(value interface{}) {
func (f *JSONFormatter) printValue(value any) {
switch value.(type) {
case string:
f.line.val = value
@@ -434,9 +434,9 @@ func (f *JSONFormatter) print(a string) {
f.line.buffer.WriteString(a)
}
func (f *JSONFormatter) printRecursive(name string, value interface{}, change ChangeType) {
func (f *JSONFormatter) printRecursive(name string, value any, change ChangeType) {
switch value := value.(type) {
case map[string]interface{}:
case map[string]any:
f.newLine(change)
f.printKey(name)
f.print("{")
@@ -456,7 +456,7 @@ func (f *JSONFormatter) printRecursive(name string, value interface{}, change Ch
f.printComma()
f.closeLine()
case []interface{}:
case []any:
f.newLine(change)
f.printKey(name)
f.print("[")
@@ -483,7 +483,7 @@ func (f *JSONFormatter) printRecursive(name string, value interface{}, change Ch
}
}
func sortKeys(m map[string]interface{}) (keys []string) {
func sortKeys(m map[string]any) (keys []string) {
keys = make([]string, 0, len(m))
for key := range m {
keys = append(keys, key)