@@ -25,29 +25,20 @@ type chainConfigWithExportedExtra struct {
25
25
26
26
// UnmarshalJSON implements the [json.Unmarshaler] interface.
27
27
func (c * ChainConfig ) UnmarshalJSON (data []byte ) error {
28
- extras := registeredExtras
29
-
30
- if extras != nil && ! extras .reuseJSONRoot {
28
+ switch reg := registeredExtras ; {
29
+ case reg != nil && ! reg .reuseJSONRoot :
31
30
return c .unmarshalJSONWithExtra (data )
32
- }
33
-
34
- if err := json .Unmarshal (data , (* chainConfigWithoutMethods )(c )); err != nil {
35
- return err
36
- }
37
- if extras == nil {
38
- return nil
39
- }
40
-
41
- // Invariants if here:
42
- // - reg.reuseJSONRoot == true
43
- // - Non-extra ChainConfig fields already unmarshalled
44
31
45
- c .extra = extras .chainConfig .NilPointer ()
46
- if err := json .Unmarshal (data , c .extra ); err != nil {
47
- c .extra = nil
48
- return err
32
+ case reg != nil && reg .reuseJSONRoot : // although the latter is redundant, it's clearer
33
+ c .extra = reg .chainConfig .NilPointer ()
34
+ if err := json .Unmarshal (data , c .extra ); err != nil {
35
+ c .extra = nil
36
+ return err
37
+ }
38
+ fallthrough // Important! We've only unmarshalled the extra field.
39
+ default : // reg == nil
40
+ return json .Unmarshal (data , (* chainConfigWithoutMethods )(c ))
49
41
}
50
- return nil
51
42
}
52
43
53
44
// unmarshalJSONWithExtra unmarshals JSON under the assumption that the
@@ -67,14 +58,14 @@ func (c *ChainConfig) unmarshalJSONWithExtra(data []byte) error {
67
58
68
59
// MarshalJSON implements the [json.Marshaler] interface.
69
60
func (c * ChainConfig ) MarshalJSON () ([]byte , error ) {
70
- switch extras := registeredExtras ; {
71
- case extras == nil :
61
+ switch reg := registeredExtras ; {
62
+ case reg == nil :
72
63
return json .Marshal ((* chainConfigWithoutMethods )(c ))
73
64
74
- case ! extras .reuseJSONRoot :
65
+ case ! reg .reuseJSONRoot :
75
66
return c .marshalJSONWithExtra ()
76
67
77
- default :
68
+ default : // reg.reuseJSONRoot == true
78
69
// The inverse of reusing the JSON root is merging two JSON buffers,
79
70
// which isn't supported by the native package. So we use
80
71
// map[string]json.RawMessage intermediates.
0 commit comments