mirror of
https://github.com/grafana/grafana.git
synced 2025-07-31 05:02:35 +08:00
Chore: use any rather than interface{} (#74066)
This commit is contained in:
@ -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)
|
||||
|
Reference in New Issue
Block a user