@@ -10,6 +10,7 @@ import (
1010 "time"
1111
1212 "github.com/dipdup-net/go-lib/config"
13+ "github.com/go-playground/validator/v10"
1314 "github.com/iancoleman/strcase"
1415 "github.com/pkg/errors"
1516
@@ -40,23 +41,37 @@ func checkHealth(ctx context.Context, api *API) {
4041 }
4142}
4243
44+ // GenerateArgs -
45+ type GenerateArgs struct {
46+ Config * config.Hasura `validate:"required"`
47+ DatabaseConfig config.Database `validate:"required"`
48+ Views []string `validate:"omitempty"`
49+ CustomConfigurations []Request `validate:"omitempty"`
50+ Models []any `validate:"omitempty"`
51+ }
52+
4353// Create - creates hasura models
44- func Create (ctx context.Context , hasura * config. Hasura , cfg config. Database , views [] string , custom [] Request , models ... interface {} ) error {
45- if hasura == nil {
54+ func Create (ctx context.Context , args GenerateArgs ) error {
55+ if args . Config == nil {
4656 return nil
4757 }
48- api := New (hasura .URL , hasura .Secret )
58+
59+ if err := validator .New ().Struct (args ); err != nil {
60+ return err
61+ }
62+
63+ api := New (args .Config .URL , args .Config .Secret )
4964
5065 checkHealth (ctx , api )
5166
52- if hasura .AddSource {
67+ if args . Config .AddSource {
5368 log .Info ().Msg ("Adding source..." )
54- if err := api .AddSource (ctx , hasura , cfg ); err != nil {
69+ if err := api .AddSource (ctx , args . Config , args . DatabaseConfig ); err != nil {
5570 return err
5671 }
5772 }
5873
59- metadata , err := Generate (* hasura , cfg , models ... )
74+ metadata , err := Generate (* args . Config , args . DatabaseConfig , args . Models ... )
6075 if err != nil {
6176 return err
6277 }
@@ -70,13 +85,13 @@ func Create(ctx context.Context, hasura *config.Hasura, cfg config.Database, vie
7085 // Find our source in the existing metadata
7186 var selectedSource * Source = nil
7287 for idx := range export .Sources {
73- if export .Sources [idx ].Name == hasura .Source {
88+ if export .Sources [idx ].Name == args . Config .Source {
7489 selectedSource = & export .Sources [idx ]
7590 break
7691 }
7792 }
7893 if selectedSource == nil {
79- return errors .Errorf ("Source '%s' not found on exported metadata" , hasura .Source )
94+ return errors .Errorf ("Source '%s' not found on exported metadata" , args . Config .Source )
8095 }
8196
8297 log .Info ().Msg ("Merging metadata..." )
@@ -97,7 +112,7 @@ func Create(ctx context.Context, hasura *config.Hasura, cfg config.Database, vie
97112 return err
98113 }
99114
100- if len (export .QueryCollections ) > 0 && (hasura . Rest == nil || * hasura .Rest ) {
115+ if len (export .QueryCollections ) > 0 && (args . Config . Rest == nil || * args . Config .Rest ) {
101116 log .Info ().Msg ("Creating REST endpoints..." )
102117 for _ , query := range export .QueryCollections [0 ].Definition .Queries {
103118 if err := api .CreateRestEndpoint (ctx , query .Name , query .Name , query .Name , allowedQueries ); err != nil {
@@ -109,18 +124,18 @@ func Create(ctx context.Context, hasura *config.Hasura, cfg config.Database, vie
109124 }
110125
111126 log .Info ().Msg ("Tracking views..." )
112- for i := range views {
113- if err := api .TrackTable (ctx , views [i ], hasura .Source ); err != nil {
127+ for i := range args . Views {
128+ if err := api .TrackTable (ctx , args . Views [i ], args . Config .Source ); err != nil {
114129 if ! strings .Contains (err .Error (), "view/table already tracked" ) {
115130 return err
116131 }
117132 }
118- if err := api .DropSelectPermissions (ctx , views [i ], hasura .Source , "user" ); err != nil {
133+ if err := api .DropSelectPermissions (ctx , args . Views [i ], args . Config .Source , "user" ); err != nil {
119134 log .Warn ().Err (err ).Msg ("" )
120135 }
121- if err := api .CreateSelectPermissions (ctx , views [i ], hasura .Source , "user" , Permission {
122- Limit : hasura .RowsLimit ,
123- AllowAggs : hasura .EnableAggregations ,
136+ if err := api .CreateSelectPermissions (ctx , args . Views [i ], args . Config .Source , "user" , Permission {
137+ Limit : args . Config .RowsLimit ,
138+ AllowAggs : args . Config .EnableAggregations ,
124139 Columns : Columns {"*" },
125140 Filter : map [string ]interface {}{},
126141 }); err != nil {
@@ -129,7 +144,7 @@ func Create(ctx context.Context, hasura *config.Hasura, cfg config.Database, vie
129144 }
130145
131146 log .Info ().Msg ("Running custom configurations..." )
132- for _ , conf := range custom {
147+ for _ , conf := range args . CustomConfigurations {
133148 if err := api .CustomConfiguration (ctx , conf ); err != nil {
134149 log .Warn ().Err (err ).Msg ("" )
135150 }
0 commit comments