-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Adds @height and contextual variables tests. This commit adds an @height variable at the request of the Truflation team. It also adds an integration test for testing contextual variables. Finally, it fixes two bugs. The first one caused SQL generated for actions to not use the `current_setting` postgres function, which caused it to incorrectly read contextual variables. The second bug caused postgres to incorrectly decode the transaction signer, essentially yielding an incorrect signer for the @signer variable. fix lint * made json util more flexible * fixed ci
- Loading branch information
Showing
21 changed files
with
314 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// package json includes JSON utilities commonly used in Kwil. | ||
package json | ||
|
||
import ( | ||
"encoding/json" | ||
"reflect" | ||
"strings" | ||
) | ||
|
||
// UnmarshalMapWithoutFloat unmarshals a JSON byte slice into a slice of maps. | ||
// It will try to convert all return values into ints, but will keep them as strings if it fails. | ||
// It ensures they aren't returned as floats, which is important for maintaining consistency | ||
// with Kwil's decimal types. All returned types will be string or int64. | ||
func UnmarshalMapWithoutFloat(b []byte) ([]map[string]any, error) { | ||
d := json.NewDecoder(strings.NewReader(string(b))) | ||
d.UseNumber() | ||
|
||
// unmashal result | ||
var result []map[string]any | ||
err := d.Decode(&result) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// convert numbers to int64 | ||
result = convertJsonNumbers(result).([]map[string]any) | ||
|
||
return result, nil | ||
} | ||
|
||
// convertJsonNumbers recursively converts json.Number to int64. | ||
// It traverses through the map and array and converts all json.Number to int64. | ||
func convertJsonNumbers(val any) any { | ||
if val == nil { | ||
return nil | ||
} | ||
switch val := val.(type) { | ||
case map[string]any: | ||
for k, v := range val { | ||
val[k] = convertJsonNumbers(v) | ||
} | ||
return val | ||
case []map[string]any: | ||
for i, v := range val { | ||
for j, n := range v { | ||
v[j] = convertJsonNumbers(n) | ||
} | ||
val[i] = v | ||
} | ||
return val | ||
case []any: | ||
for i, v := range val { | ||
val[i] = convertJsonNumbers(v) | ||
} | ||
return val | ||
case json.Number: | ||
i, err := val.Int64() | ||
if err != nil { | ||
return val.String() | ||
} | ||
return i | ||
default: | ||
// in case we are unmarshalling something crazy like a double nested slice, | ||
// we reflect on the value and recursively call convertJsonNumbers if it's a slice. | ||
typeOf := reflect.TypeOf(val) | ||
if typeOf.Kind() == reflect.Slice { | ||
s := reflect.ValueOf(val) | ||
for i := 0; i < s.Len(); i++ { | ||
s.Index(i).Set(reflect.ValueOf(convertJsonNumbers(s.Index(i).Interface()))) | ||
} | ||
return s.Interface() | ||
} | ||
return val | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package json | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func Test_convertJsonNumbers(t *testing.T) { | ||
|
||
tests := []struct { | ||
name string | ||
val any | ||
want any | ||
}{ | ||
{ | ||
name: "number", | ||
val: json.Number("123"), | ||
want: int64(123), | ||
}, | ||
{ | ||
name: "object", | ||
val: map[string]any{ | ||
"key": json.Number("123"), | ||
"val": []map[string]any{ | ||
{ | ||
"key": json.Number("123"), | ||
}, | ||
{ | ||
"key": json.Number("456"), | ||
"val": []map[string]any{ | ||
{ | ||
"key": json.Number("789"), | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
want: map[string]any{ | ||
"key": int64(123), | ||
"val": []map[string]any{ | ||
{ | ||
"key": int64(123), | ||
}, | ||
{ | ||
"key": int64(456), | ||
"val": []map[string]any{ | ||
{ | ||
"key": int64(789), | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
a := convertJsonNumbers(tt.val) | ||
require.EqualValues(t, tt.want, a) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.