Skip to content

Commit 0a1027d

Browse files
Crypto and some minor ifxes
1 parent 9028690 commit 0a1027d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+1624
-448
lines changed

config/config.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,17 @@ type Config struct {
2121

2222
// Substitute -
2323
func (c *Config) Substitute() error {
24-
c.Hasura.SetSourceName()
24+
if c.Hasura != nil {
25+
c.Hasura.SetSourceName()
26+
}
2527
return nil
2628
}
2729

2830
// DataSource -
2931
type DataSource struct {
30-
Kind string `yaml:"kind"`
31-
URL string `yaml:"url" validate:"required,url"`
32+
Kind string `yaml:"kind"`
33+
URL string `yaml:"url" validate:"required,url"`
34+
Credentials *Credentials `yaml:"credentials,omitempty" validate:"omitempty"`
3235
}
3336

3437
// Contracts -
@@ -61,6 +64,9 @@ type Hasura struct {
6164
}
6265

6366
func (s *Hasura) SetSourceName() {
67+
if s == nil {
68+
return
69+
}
6470
if s.Source == "" {
6571
s.Source = "default"
6672
}

config/credentials.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package config
2+
3+
// Credentials -
4+
type Credentials struct {
5+
User *UserCredentials `yaml:"user,omitempty" validate:"omitempty"`
6+
}
7+
8+
// UserCredentials -
9+
type UserCredentials struct {
10+
Name string `yaml:"name" validate:"required"`
11+
Password string `yaml:"password" validate:"required"`
12+
}

hasura/requests.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package hasura
22

3-
import "github.com/pkg/errors"
3+
import (
4+
"os"
5+
6+
"github.com/pkg/errors"
7+
)
48

59
type Request struct {
610
Type string `json:"type"`
@@ -69,7 +73,11 @@ func (d *DatabaseUrl) UnmarshalJSON(data []byte) error {
6973
return err
7074
}
7175

72-
*d = DatabaseUrl(fromEnv.FromEnv)
76+
fromEnvValue := os.Getenv(fromEnv.FromEnv)
77+
if fromEnvValue == "" {
78+
return errors.Errorf("you have to set '%s' environment variable due to hasura connection info", fromEnv.FromEnv)
79+
}
80+
*d = DatabaseUrl(fromEnvValue)
7381
return nil
7482
}
7583

tools/ast/ast.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -261,8 +261,16 @@ func (a *TypedAst) Docs(entrypoint string) ([]Typedef, error) {
261261

262262
node := a.FindByName(entrypoint, true)
263263
if node != nil {
264-
docs, _, err := node.Docs(DocsFull)
265-
return docs, err
264+
docs, typName, err := node.Docs(DocsFull)
265+
if docs != nil {
266+
return docs, err
267+
}
268+
return []Typedef{
269+
{
270+
Name: entrypoint,
271+
Type: typName,
272+
},
273+
}, nil
266274
}
267275
return nil, nil
268276
}
@@ -342,7 +350,7 @@ func (a *TypedAst) Diff(b *TypedAst) (*MiguelNode, error) {
342350
return nil, nil
343351
}
344352

345-
// FromParameters - fill(settle) subtree in `a` with `data`. Returned settled subtree and error if occured. If `entrypoint` is empty string it will try to settle main tree.
353+
// FromParameters - fill(settle) subtree in `a` with `data`. Returned settled subtree and error if occurred. If `entrypoint` is empty string it will try to settle main tree.
346354
func (a *TypedAst) FromParameters(data *types.Parameters) (*TypedAst, error) {
347355
var tree UntypedAST
348356
if err := json.Unmarshal(data.Value, &tree); err != nil {
@@ -423,7 +431,7 @@ func (a *TypedAst) GetJSONModel(model JSONModel) {
423431
}
424432
}
425433

426-
// Unwrap - clear paramaters from Left/Right. Tree must be settled.
434+
// Unwrap - clear parameters from Left/Right. Tree must be settled.
427435
func (a *TypedAst) UnwrapAndGetEntrypointName() (Node, string) {
428436
if !a.IsSettled() || len(a.Nodes) != 1 {
429437
return nil, ""

tools/ast/ast_test.go

Lines changed: 95 additions & 18 deletions
Large diffs are not rendered by default.

tools/ast/big_map.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ func (m *BigMap) ToJSONSchema() (*JSONSchema, error) {
167167
Type: JSONSchemaTypeArray,
168168
Title: m.GetName(),
169169
Default: make([]interface{}, 0),
170-
Items: &SchemaKey{
170+
Items: &JSONSchema{
171171
Type: JSONSchemaTypeObject,
172172
Required: make([]string, 0),
173173
Properties: make(map[string]*JSONSchema),
@@ -178,7 +178,13 @@ func (m *BigMap) ToJSONSchema() (*JSONSchema, error) {
178178
return nil, err
179179
}
180180

181-
if err := setChildSchemaForMap(m.ValueType, false, s); err != nil {
181+
if m.ValueType.IsPrim(consts.PAIR) {
182+
child, err := m.ValueType.ToJSONSchema()
183+
if err != nil {
184+
return nil, err
185+
}
186+
s.Items.Properties[m.ValueType.GetName()] = child
187+
} else if err := setChildSchemaForMap(m.ValueType, false, s); err != nil {
182188
return nil, err
183189
}
184190

tools/ast/bytes.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,7 @@ func (b *Bytes) Distinguish(x Distinguishable) (*MiguelNode, error) {
5151

5252
// FromJSONSchema -
5353
func (b *Bytes) FromJSONSchema(data map[string]interface{}) error {
54-
setBytesJSONSchema(&b.Default, data)
55-
return nil
54+
return setBytesJSONSchema(&b.Default, data)
5655
}
5756

5857
// FindByName -

tools/ast/chest.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package ast
2+
3+
import (
4+
"encoding/hex"
5+
"strings"
6+
"unicode/utf8"
7+
8+
"github.com/dipdup-net/go-lib/tools/consts"
9+
"github.com/dipdup-net/go-lib/tools/forge"
10+
"github.com/dipdup-net/go-lib/tools/formatter"
11+
)
12+
13+
// Chest -
14+
type Chest struct {
15+
Default
16+
}
17+
18+
// NewChest -
19+
func NewChest(depth int) *Chest {
20+
return &Chest{
21+
Default: NewDefault(consts.CHEST, 0, depth),
22+
}
23+
}
24+
25+
// ToJSONSchema -
26+
func (c *Chest) ToJSONSchema() (*JSONSchema, error) {
27+
return getStringJSONSchema(c.Default), nil
28+
}
29+
30+
// Compare -
31+
func (c *Chest) Compare(second Comparable) (int, error) {
32+
s, ok := second.(*Chest)
33+
if !ok {
34+
return 0, consts.ErrTypeIsNotComparable
35+
}
36+
return strings.Compare(c.Value.(string), s.Value.(string)), nil
37+
}
38+
39+
// Distinguish -
40+
func (c *Chest) Distinguish(x Distinguishable) (*MiguelNode, error) {
41+
second, ok := x.(*Chest)
42+
if !ok {
43+
return nil, nil
44+
}
45+
46+
return c.Default.Distinguish(&second.Default)
47+
}
48+
49+
// FromJSONSchema -
50+
func (c *Chest) FromJSONSchema(data map[string]interface{}) error {
51+
return setBytesJSONSchema(&c.Default, data)
52+
}
53+
54+
// FindByName -
55+
func (c *Chest) FindByName(name string, isEntrypoint bool) Node {
56+
if c.GetName() == name {
57+
return c
58+
}
59+
return nil
60+
}
61+
62+
// ToMiguel -
63+
func (c *Chest) ToMiguel() (*MiguelNode, error) {
64+
node, err := c.Default.ToMiguel()
65+
if err != nil {
66+
return nil, err
67+
}
68+
69+
if str, ok := node.Value.(string); ok {
70+
tree := forge.TryUnpackString(str)
71+
if tree != nil {
72+
treeJSON, err := json.MarshalToString(tree)
73+
if err == nil {
74+
node.Value, _ = formatter.MichelineToMichelsonInline(treeJSON)
75+
}
76+
} else if data, err := hex.DecodeString(str); err == nil && utf8.Valid(data) {
77+
node.Value = string(data)
78+
}
79+
}
80+
81+
return node, nil
82+
}

tools/ast/chest_key.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package ast
2+
3+
import (
4+
"encoding/hex"
5+
"strings"
6+
"unicode/utf8"
7+
8+
"github.com/dipdup-net/go-lib/tools/consts"
9+
"github.com/dipdup-net/go-lib/tools/forge"
10+
"github.com/dipdup-net/go-lib/tools/formatter"
11+
)
12+
13+
// ChestKey -
14+
type ChestKey struct {
15+
Default
16+
}
17+
18+
// NewChestKey -
19+
func NewChestKey(depth int) *ChestKey {
20+
return &ChestKey{
21+
Default: NewDefault(consts.CHESTKEY, 0, depth),
22+
}
23+
}
24+
25+
// ToJSONSchema -
26+
func (c *ChestKey) ToJSONSchema() (*JSONSchema, error) {
27+
return getStringJSONSchema(c.Default), nil
28+
}
29+
30+
// Compare -
31+
func (c *ChestKey) Compare(second Comparable) (int, error) {
32+
s, ok := second.(*ChestKey)
33+
if !ok {
34+
return 0, consts.ErrTypeIsNotComparable
35+
}
36+
return strings.Compare(c.Value.(string), s.Value.(string)), nil
37+
}
38+
39+
// Distinguish -
40+
func (c *ChestKey) Distinguish(x Distinguishable) (*MiguelNode, error) {
41+
second, ok := x.(*ChestKey)
42+
if !ok {
43+
return nil, nil
44+
}
45+
46+
return c.Default.Distinguish(&second.Default)
47+
}
48+
49+
// FromJSONSchema -
50+
func (c *ChestKey) FromJSONSchema(data map[string]interface{}) error {
51+
return setBytesJSONSchema(&c.Default, data)
52+
}
53+
54+
// FindByName -
55+
func (c *ChestKey) FindByName(name string, isEntrypoint bool) Node {
56+
if c.GetName() == name {
57+
return c
58+
}
59+
return nil
60+
}
61+
62+
// ToMiguel -
63+
func (c *ChestKey) ToMiguel() (*MiguelNode, error) {
64+
node, err := c.Default.ToMiguel()
65+
if err != nil {
66+
return nil, err
67+
}
68+
69+
if str, ok := node.Value.(string); ok {
70+
tree := forge.TryUnpackString(str)
71+
if tree != nil {
72+
treeJSON, err := json.MarshalToString(tree)
73+
if err == nil {
74+
node.Value, _ = formatter.MichelineToMichelsonInline(treeJSON)
75+
}
76+
} else if data, err := hex.DecodeString(str); err == nil && utf8.Valid(data) {
77+
node.Value = string(data)
78+
}
79+
}
80+
81+
return node, nil
82+
}

0 commit comments

Comments
 (0)