Skip to content

Commit

Permalink
Change interface{} to any for better readability
Browse files Browse the repository at this point in the history
  • Loading branch information
weiihann committed Sep 27, 2024
1 parent 58e9316 commit fbfd89d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 23 deletions.
20 changes: 10 additions & 10 deletions core/class.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -297,15 +297,15 @@ 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 {
return "", err
}

// Determine the type of the JSON data
var jsonData interface{}
var jsonData any
if err := json.Unmarshal(jsonBytes, &jsonData); err != nil {
return "", err
}
Expand Down
26 changes: 13 additions & 13 deletions core/program.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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.
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit fbfd89d

Please sign in to comment.