@agoric/cosmos 0.34.2-dev-7bf5870.0 → 0.34.2-dev-121b677.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/git-revision.txt CHANGED
@@ -1 +1 @@
1
- 7bf5870
1
+ 121b677
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@agoric/cosmos",
3
- "version": "0.34.2-dev-7bf5870.0+7bf5870",
3
+ "version": "0.34.2-dev-121b677.0+121b677",
4
4
  "description": "Connect JS to the Cosmos blockchain SDK",
5
5
  "parsers": {
6
6
  "js": "mjs"
@@ -38,5 +38,5 @@
38
38
  "typeCoverage": {
39
39
  "atLeast": 0
40
40
  },
41
- "gitHead": "7bf587060268281ef4b53ed89cefccfd9cd3f8ea"
41
+ "gitHead": "121b67747aaa842adff618c610dbb0835d88cc33"
42
42
  }
package/vm/action.go CHANGED
@@ -58,21 +58,23 @@ func SetActionHeaderFromContext(ctx sdk.Context, actionType string, ah *ActionHe
58
58
  }
59
59
  }
60
60
 
61
- // PopulateAction interprets `default:"..."` tags and specially handles an
62
- // embedded ActionHeader struct.
61
+ // PopulateAction returns a clone of action in which empty/zero-valued fields
62
+ // in its embedded ActionHeader have been populated using the corresponding
63
+ // `actionType:"..."` tag and the provided ctx, and its own empty/zero-valued
64
+ // fields have been populated as specified by their `default:"..."` tags.
63
65
  func PopulateAction(ctx sdk.Context, action Action) Action {
64
- oldRv := reflect.Indirect(reflect.ValueOf(action))
65
- if oldRv.Kind() != reflect.Struct {
66
+ oldActionDesc := reflect.Indirect(reflect.ValueOf(action))
67
+ if oldActionDesc.Kind() != reflect.Struct {
66
68
  return action
67
69
  }
68
70
 
69
71
  // Shallow copy to a new value.
70
- rp := reflect.New(oldRv.Type())
71
- rv := reflect.Indirect(rp)
72
- for i := 0; i < rv.NumField(); i++ {
73
- oldField := oldRv.Field(i)
74
- field := rv.Field(i)
75
- fieldType := rv.Type().Field(i)
72
+ newActionDescPtr := reflect.New(oldActionDesc.Type())
73
+ newActionDesc := reflect.Indirect(newActionDescPtr)
74
+ for i := 0; i < newActionDesc.NumField(); i++ {
75
+ oldField := oldActionDesc.Field(i)
76
+ field := newActionDesc.Field(i)
77
+ fieldType := newActionDesc.Type().Field(i)
76
78
  if !field.CanSet() {
77
79
  continue
78
80
  }
@@ -81,34 +83,35 @@ func PopulateAction(ctx sdk.Context, action Action) Action {
81
83
  field.Set(oldField)
82
84
 
83
85
  // Populate any ActionHeader struct.
84
- var ahp *ActionHeader
86
+ var headerPtr *ActionHeader
85
87
  if fieldType.Type == actionHeaderType {
86
- ahp = field.Addr().Interface().(*ActionHeader)
88
+ headerPtr = field.Addr().Interface().(*ActionHeader)
87
89
  } else if fieldType.Type == reflect.PtrTo(actionHeaderType) {
88
90
  if field.IsNil() {
89
- ahp = &ActionHeader{}
91
+ headerPtr = &ActionHeader{}
90
92
  } else {
91
- ahp = field.Interface().(*ActionHeader)
93
+ headerPtr = field.Interface().(*ActionHeader)
92
94
  }
93
95
  }
94
- if ahp != nil {
96
+ if headerPtr != nil {
95
97
  actionTypeTag, _ := fieldType.Tag.Lookup("actionType")
96
- ah := *ahp
97
- SetActionHeaderFromContext(ctx, actionTypeTag, &ah)
98
+ newHeader := *headerPtr
99
+ SetActionHeaderFromContext(ctx, actionTypeTag, &newHeader)
98
100
  if field.Kind() == reflect.Ptr {
99
- field.Set(reflect.ValueOf(&ah))
101
+ field.Set(reflect.ValueOf(&newHeader))
100
102
  } else {
101
- field.Set(reflect.ValueOf(ah))
103
+ field.Set(reflect.ValueOf(newHeader))
102
104
  }
103
105
  continue
104
106
  }
105
107
 
106
- // Still zero value, try default struct field tag.
108
+ // Skip any field that is already populated or lacks a "default" tag.
107
109
  defaultTag, _ := fieldType.Tag.Lookup("default")
108
110
  if !field.IsZero() || len(defaultTag) == 0 {
109
111
  continue
110
112
  }
111
113
 
114
+ // Populate the field from its "default" tag.
112
115
  switch field.Kind() {
113
116
  case reflect.String:
114
117
  field.SetString(defaultTag)
@@ -126,5 +129,5 @@ func PopulateAction(ctx sdk.Context, action Action) Action {
126
129
  }
127
130
  }
128
131
  }
129
- return rv.Interface().(Action)
132
+ return newActionDesc.Interface().(Action)
130
133
  }
package/vm/action_test.go CHANGED
@@ -106,21 +106,21 @@ func TestActionContext(t *testing.T) {
106
106
  for _, tc := range testCases {
107
107
  tc := tc
108
108
  t.Run(tc.name, func(t *testing.T) {
109
- jstr := func(in interface{}) string {
109
+ toJson := func(in interface{}) string {
110
110
  bz, err := json.Marshal(in)
111
111
  if err != nil {
112
112
  t.Fatal(err)
113
113
  }
114
114
  return string(bz)
115
115
  }
116
- jsonIn := jstr(tc.in)
116
+ jsonIn := toJson(tc.in)
117
117
  out := vm.PopulateAction(tc.ctx, tc.in)
118
- jsonIn2 := jstr(tc.in)
118
+ jsonIn2 := toJson(tc.in)
119
119
  if jsonIn != jsonIn2 {
120
120
  t.Errorf("unexpected mutated input: %s to %s", jsonIn, jsonIn2)
121
121
  }
122
- jsonOut := jstr(out)
123
- jsonExpectedOut := jstr(tc.expectedOut)
122
+ jsonOut := toJson(out)
123
+ jsonExpectedOut := toJson(tc.expectedOut)
124
124
  if jsonOut != jsonExpectedOut {
125
125
  t.Errorf("expected %s, got %s", jsonExpectedOut, jsonOut)
126
126
  }