From fbfd89d3ab2f2c7ecdb02ea3d6262bae56c7844a Mon Sep 17 00:00:00 2001 From: weiihann Date: Fri, 27 Sep 2024 20:10:23 +0800 Subject: [PATCH] Change interface{} to any for better readability --- core/class.go | 20 ++++++++++---------- core/program.go | 26 +++++++++++++------------- 2 files changed, 23 insertions(+), 23 deletions(-) diff --git a/core/class.go b/core/class.go index 70018b5a38..5c464d4dd9 100644 --- a/core/class.go +++ b/core/class.go @@ -231,9 +231,9 @@ func makeDeprecatedVMClass(class *Cairo0Class) (*starknet.Cairo0Definition, erro } // applyReplacer recursively applies the replacer function to the JSON data -func applyReplacer(data interface{}, replacer func(string, interface{}) interface{}) interface{} { +func applyReplacer(data any, replacer func(string, any) any) any { switch v := data.(type) { - case map[string]interface{}: + case map[string]any: for key, val := range v { v[key] = applyReplacer(replacer(key, val), replacer) if v[key] == nil && key != debugInfo { @@ -242,12 +242,12 @@ func applyReplacer(data interface{}, replacer func(string, interface{}) interfac } return v - case []interface{}: + case []any: for i, val := range v { v[i] = applyReplacer(replacer("", val), replacer) } return v - case *orderedmap.OrderedMap[string, interface{}]: + case *orderedmap.OrderedMap[string, any]: for pair := v.Oldest(); pair != nil; pair = pair.Next() { val := applyReplacer(replacer(pair.Key, pair.Value), replacer) if val == nil { @@ -263,10 +263,10 @@ func applyReplacer(data interface{}, replacer func(string, interface{}) interfac } // nullSkipReplacer is a custom JSON replacer that handles specific keys and null values -func nullSkipReplacer(key string, value interface{}) interface{} { +func nullSkipReplacer(key string, value any) any { switch key { case "attributes", "accessible_scopes", "flow_tracking_data": - if arr, ok := value.([]interface{}); ok && len(arr) == 0 { + if arr, ok := value.([]any); ok && len(arr) == 0 { return nil } case debugInfo: @@ -279,14 +279,14 @@ func nullSkipReplacer(key string, value interface{}) interface{} { // identifiersNullSkipReplacer is same as nullSkipReplacer with an addition condition // on the `cairo_type` field. // Used only in the `identifiers` field. -func identifiersNullSkipReplacer(key string, value interface{}) interface{} { +func identifiersNullSkipReplacer(key string, value any) any { switch key { case "cairo_type": if str, ok := value.(string); ok { return strings.ReplaceAll(str, ": ", " : ") } case "attributes", "accessible_scopes", "flow_tracking_data": - if arr, ok := value.([]interface{}); ok && len(arr) == 0 { + if arr, ok := value.([]any); ok && len(arr) == 0 { return nil } case debugInfo: @@ -297,7 +297,7 @@ func identifiersNullSkipReplacer(key string, value interface{}) interface{} { } // stringify converts a Go value to a JSON string, using a custom replacer function -func stringify(value interface{}, replacer func(string, interface{}) interface{}) (string, error) { +func stringify(value any, replacer func(string, any) any) (string, error) { // Marshal the value to JSON jsonBytes, err := json.Marshal(value) if err != nil { @@ -305,7 +305,7 @@ func stringify(value interface{}, replacer func(string, interface{}) interface{} } // Determine the type of the JSON data - var jsonData interface{} + var jsonData any if err := json.Unmarshal(jsonBytes, &jsonData); err != nil { return "", err } diff --git a/core/program.go b/core/program.go index d48577b740..f1f533ad88 100644 --- a/core/program.go +++ b/core/program.go @@ -9,20 +9,20 @@ import ( ) type Program struct { - Attributes []interface{} `json:"attributes,omitempty"` - Builtins []string `json:"builtins"` - CompilerVersion interface{} `json:"compiler_version,omitempty"` - Data []string `json:"data"` - DebugInfo interface{} `json:"debug_info"` - Hints *orderedmap.OrderedMap[string, interface{}] `json:"hints,omitempty"` - Identifiers interface{} `json:"identifiers,omitempty"` - MainScope interface{} `json:"main_scope,omitempty"` - Prime interface{} `json:"prime,omitempty"` - ReferenceManager interface{} `json:"reference_manager"` + Attributes []any `json:"attributes,omitempty"` + Builtins []string `json:"builtins"` + CompilerVersion any `json:"compiler_version,omitempty"` + Data []string `json:"data"` + DebugInfo any `json:"debug_info"` + Hints *orderedmap.OrderedMap[string, any] `json:"hints,omitempty"` + Identifiers any `json:"identifiers,omitempty"` + MainScope any `json:"main_scope,omitempty"` + Prime any `json:"prime,omitempty"` + ReferenceManager any `json:"reference_manager"` } func (p *Program) Format() error { - p.Attributes = applyReplacer(p.Attributes, nullSkipReplacer).([]interface{}) + p.Attributes = applyReplacer(p.Attributes, nullSkipReplacer).([]any) if len(p.Attributes) == 0 { p.Attributes = nil } @@ -36,7 +36,7 @@ func (p *Program) Format() error { if err := p.ReorderHints(); err != nil { return err } - p.Hints = applyReplacer(p.Hints, nullSkipReplacer).(*orderedmap.OrderedMap[string, interface{}]) + p.Hints = applyReplacer(p.Hints, nullSkipReplacer).(*orderedmap.OrderedMap[string, any]) if p.CompilerVersion != nil { // Anything since compiler version 0.10.0 can be hashed directly. No extra overhead incurred. @@ -69,7 +69,7 @@ func (p *Program) ReorderHints() error { sort.Ints(intKeys) // Rebuild the OrderedMap using sorted keys - newHints := orderedmap.New[string, interface{}]() + newHints := orderedmap.New[string, any]() for _, intKey := range intKeys { strKey := strconv.Itoa(intKey) value, _ := p.Hints.Get(strKey)