Skip to content

Commit 1c916a7

Browse files
committed
feat: update types filter
1 parent 89b0696 commit 1c916a7

File tree

11 files changed

+153
-145
lines changed

11 files changed

+153
-145
lines changed

api/httpapi/handlers/asset_handler.go

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,9 @@ func (h *AssetHandler) GetAll(w http.ResponseWriter, r *http.Request) {
7171
withTotal, ok := r.URL.Query()["with_total"]
7272
if ok && len(withTotal) > 0 && withTotal[0] != "false" && withTotal[0] != "0" {
7373
total, err := h.assetRepo.GetCount(r.Context(), asset.Config{
74-
Type: cfg.Type,
75-
Service: cfg.Service,
76-
Text: cfg.Text,
74+
Types: cfg.Types,
75+
Services: cfg.Services,
76+
Text: cfg.Text,
7777
})
7878
if err != nil {
7979
internalServerError(w, h.logger, err.Error())
@@ -321,25 +321,21 @@ func (h *AssetHandler) validateAsset(ast asset.Asset) error {
321321
if ast.URN == "" {
322322
return fmt.Errorf("urn is required")
323323
}
324-
if ast.Type == "" {
324+
if ast.Types != nil {
325325
return fmt.Errorf("type is required")
326326
}
327-
if !ast.Type.IsValid() {
328-
return fmt.Errorf("type is invalid")
329-
}
327+
//if !ast.Types.IsValid() {
328+
// return fmt.Errorf("type is invalid")
329+
//}
330330
if ast.Name == "" {
331331
return fmt.Errorf("name is required")
332332
}
333333
if ast.Data == nil {
334334
return fmt.Errorf("data is required")
335335
}
336-
337-
if ast.Service == "" {
336+
if ast.Services != nil {
338337
return fmt.Errorf("service is required")
339338
}
340-
//if !ast.Service.IsValid() {
341-
// return fmt.Errorf("service is invalid")
342-
//}
343339

344340
return nil
345341
}
@@ -400,11 +396,11 @@ func (h *AssetHandler) buildAssetConfig(query url.Values) (cfg asset.Config, err
400396

401397
types := query.Get("type")
402398
if types != "" {
403-
cfg.Type = strings.Split(types, ",")
399+
cfg.Types = strings.Split(types, ",")
404400
}
405401
services := query.Get("service")
406402
if services != "" {
407-
cfg.Service = strings.Split(services, ",")
403+
cfg.Services = strings.Split(services, ",")
408404
}
409405

410406
cfg.Name = query.Get("name")
@@ -466,8 +462,8 @@ func filterConfigFromAssetValues(querystring url.Values) map[string][]string {
466462
func (h *AssetHandler) saveLineage(ctx context.Context, ast asset.Asset, upstreams, downstreams []lineage.Node) error {
467463
node := lineage.Node{
468464
URN: ast.URN,
469-
Type: ast.Type,
470-
Service: ast.Service,
465+
Type: ast.Types,
466+
Service: ast.Services,
471467
}
472468

473469
return h.lineageRepo.Upsert(ctx, node, upstreams, downstreams)

api/httpapi/handlers/asset_handler_test.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,9 @@ func TestAssetHandlerUpsert(t *testing.T) {
192192
t.Run("should return HTTP 200 and asset's ID if the asset is successfully created/updated", func(t *testing.T) {
193193
ast := asset.Asset{
194194
URN: "test dagger",
195-
Type: asset.TypeTable,
195+
Types: asset.TypeTable,
196196
Name: "de-dagger-test",
197-
Service: "kafka",
197+
Services: []string{"kafka"},
198198
UpdatedBy: user.User{ID: userID},
199199
Data: map[string]interface{}{},
200200
}
@@ -229,8 +229,8 @@ func TestAssetHandlerUpsert(t *testing.T) {
229229
lr.On("Upsert", rr.Context(),
230230
lineage.Node{
231231
URN: ast.URN,
232-
Type: ast.Type,
233-
Service: ast.Service,
232+
Type: ast.Types,
233+
Service: ast.Services,
234234
},
235235
upstreams,
236236
downstreams,
@@ -695,8 +695,8 @@ func TestAssetHandlerGet(t *testing.T) {
695695
Setup: func(ctx context.Context, ar *mocks.AssetRepository) {
696696
ar.On("GetAll", ctx, asset.Config{
697697
Text: "asd",
698-
Type: []string{"table", "job"},
699-
Service: []string{"bigquery", "presto"},
698+
Types: []string{"table", "job"},
699+
Services: []string{"bigquery", "presto"},
700700
Size: 30,
701701
Offset: 50,
702702
SortDirection: "desc",
@@ -741,20 +741,20 @@ func TestAssetHandlerGet(t *testing.T) {
741741
Querystring: "?with_total=true&text=dsa&type=job&service=kafka&size=10&offset=5",
742742
Setup: func(ctx context.Context, ar *mocks.AssetRepository) {
743743
ar.On("GetAll", ctx, asset.Config{
744-
Text: "dsa",
745-
Type: []string{"job"},
746-
Service: []string{"kafka"},
747-
Size: 10,
748-
Offset: 5,
744+
Text: "dsa",
745+
Types: []string{"job"},
746+
Services: []string{"kafka"},
747+
Size: 10,
748+
Offset: 5,
749749
}).Return([]asset.Asset{
750750
{ID: "testid-1"},
751751
{ID: "testid-2"},
752752
{ID: "testid-3"},
753753
}, nil, nil)
754754
ar.On("GetCount", ctx, asset.Config{
755-
Text: "dsa",
756-
Type: []string{"job"},
757-
Service: []string{"kafka"},
755+
Text: "dsa",
756+
Types: []string{"job"},
757+
Services: []string{"kafka"},
758758
}).Return(150, nil, nil)
759759
},
760760
PostCheck: func(r *http.Response) error {

api/httpapi/handlers/record_handler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ func (h *RecordHandler) validateRecord(ast asset.Asset) error {
233233
if ast.Data == nil {
234234
return fmt.Errorf("data is required")
235235
}
236-
if ast.Service == "" {
236+
if ast.Services != nil {
237237
return fmt.Errorf("service is required")
238238
}
239239

asset/asset.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ type Repository interface {
2727
type Asset struct {
2828
ID string `json:"id" diff:"-"`
2929
URN string `json:"urn" diff:"-"`
30-
Type Type `json:"type" diff:"-"`
31-
Service string `json:"service" diff:"-"`
30+
Types []Type `json:"type" diff:"-"`
31+
Services []string `json:"service" diff:"-"`
3232
Name string `json:"name" diff:"name"`
3333
Description string `json:"description" diff:"description"`
3434
Data map[string]interface{} `json:"data" diff:"data"`

asset/config.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,19 @@ func init() {
2222
}
2323

2424
type RecordFilter map[string][]string
25+
type ConfigType []Type
2526

2627
type Config struct {
27-
Text string `json:"text"`
28-
Type []string `json:"type"`
29-
Service []string `json:"service"`
30-
Size int `json:"size"`
31-
Offset int `json:"offset"`
32-
SortBy string `json:"sort" validate:"omitempty,oneof=recent created_at updated_at"`
33-
SortDirection string `json:"direction" validate:"omitempty,oneof=asc desc"`
28+
Text string
29+
Types ConfigType
30+
Services []string
31+
Size int
32+
Offset int
33+
SortBy string `validate:"omitempty,oneof=name types services created_at updated_at"`
34+
SortDirection string `validate:"omitempty,oneof=asc desc"`
3435
Filters RecordFilter
35-
TypeWhiteList []string
36-
URN string `json:"urn"`
37-
Name string `json:"name"`
36+
URN string
37+
Name string
3838
}
3939

4040
// Validate will check whether fields in the filter fulfills the constraint

asset/version.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ const BaseVersion = "0.1"
1515
type AssetVersion struct {
1616
ID string `json:"id" db:"id"`
1717
URN string `json:"urn" db:"urn"`
18-
Type string `json:"type" db:"type"`
19-
Service string `json:"service" db:"service"`
18+
Types []string `json:"type" db:"type"`
19+
Services []string `json:"service" db:"service"`
2020
Version string `json:"version" db:"version"`
2121
Changelog diff.Changelog `json:"changelog" db:"changelog"`
2222
UpdatedBy user.User `json:"updated_by" db:"updated_by"`

store/postgres/asset_model.go

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/json"
66
"errors"
77
"fmt"
8+
"github.com/lib/pq"
89
"time"
910

1011
"github.com/jmoiron/sqlx/types"
@@ -14,18 +15,18 @@ import (
1415
)
1516

1617
type AssetModel struct {
17-
ID string `db:"id"`
18-
URN string `db:"urn"`
19-
Type string `db:"type"`
20-
Name string `db:"name"`
21-
Service string `db:"service"`
22-
Description string `db:"description"`
23-
Data JSONMap `db:"data"`
24-
Labels JSONMap `db:"labels"`
25-
Version string `db:"version"`
26-
UpdatedBy UserModel `db:"updated_by"`
27-
CreatedAt time.Time `db:"created_at"`
28-
UpdatedAt time.Time `db:"updated_at"`
18+
ID string `db:"id"`
19+
URN string `db:"urn"`
20+
Types []string `db:"type"`
21+
Name string `db:"name"`
22+
Services pq.StringArray `db:"service"`
23+
Description string `db:"description"`
24+
Data JSONMap `db:"data"`
25+
Labels JSONMap `db:"labels"`
26+
Version string `db:"version"`
27+
UpdatedBy UserModel `db:"updated_by"`
28+
CreatedAt time.Time `db:"created_at"`
29+
UpdatedAt time.Time `db:"updated_at"`
2930
// version specific information
3031
Changelog types.JSONText `db:"changelog"`
3132
Owners types.JSONText `db:"owners"`
@@ -36,9 +37,9 @@ func (a *AssetModel) toAsset(owners []user.User) asset.Asset {
3637
return asset.Asset{
3738
ID: a.ID,
3839
URN: a.URN,
39-
Type: asset.Type(a.Type),
40+
Types: asset.Type(a.Types),
4041
Name: a.Name,
41-
Service: a.Service,
42+
Services: a.Services,
4243
Description: a.Description,
4344
Data: a.Data,
4445
Labels: a.buildLabels(),
@@ -61,8 +62,8 @@ func (a *AssetModel) toAssetVersion() (asset.AssetVersion, error) {
6162
return asset.AssetVersion{
6263
ID: a.ID,
6364
URN: a.URN,
64-
Type: a.Type,
65-
Service: a.Service,
65+
Types: a.Types,
66+
Services: a.Services,
6667
Version: a.Version,
6768
Changelog: clog,
6869
UpdatedBy: a.UpdatedBy.toUser(),
@@ -86,9 +87,9 @@ func (a *AssetModel) toVersionedAsset(latestAssetVersion asset.Asset) (asset.Ass
8687
return asset.Asset{
8788
ID: latestAssetVersion.ID,
8889
URN: latestAssetVersion.URN,
89-
Type: asset.Type(latestAssetVersion.Type),
90+
Types: latestAssetVersion.Types,
9091
Name: a.Name,
91-
Service: latestAssetVersion.Service,
92+
Services: latestAssetVersion.Services,
9293
Description: a.Description,
9394
Data: a.Data,
9495
Labels: a.buildLabels(),

store/postgres/asset_repository.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ func (r *AssetRepository) insert(ctx context.Context, ast *asset.Asset) (id stri
300300
err = r.client.RunWithinTx(ctx, func(tx *sqlx.Tx) error {
301301
query, args, err := sq.Insert("assets").
302302
Columns("urn", "type", "service", "name", "description", "data", "labels", "updated_by", "version").
303-
Values(ast.URN, ast.Type, ast.Service, ast.Name, ast.Description, ast.Data, ast.Labels, ast.UpdatedBy.ID, asset.BaseVersion).
303+
Values(ast.URN, ast.Types, ast.Services, ast.Name, ast.Description, ast.Data, ast.Labels, ast.UpdatedBy.ID, asset.BaseVersion).
304304
Suffix("RETURNING \"id\"").
305305
PlaceholderFormat(sq.Dollar).
306306
ToSql()
@@ -360,7 +360,7 @@ func (r *AssetRepository) update(ctx context.Context, assetID string, newAsset *
360360
version = $10
361361
WHERE id = $11;
362362
`,
363-
newAsset.URN, newAsset.Type, newAsset.Service, newAsset.Name, newAsset.Description, newAsset.Data, newAsset.Labels, time.Now(), newAsset.UpdatedBy.ID, newVersion, assetID)
363+
newAsset.URN, newAsset.Types, newAsset.Services, newAsset.Name, newAsset.Description, newAsset.Data, newAsset.Labels, time.Now(), newAsset.UpdatedBy.ID, newVersion, assetID)
364364
if err != nil {
365365
return fmt.Errorf("error running update asset query: %w", err)
366366
}
@@ -404,7 +404,7 @@ func (r *AssetRepository) insertAssetVersion(ctx context.Context, execer sqlx.Ex
404404
}
405405
query, args, err := sq.Insert("assets_versions").
406406
Columns("asset_id", "urn", "type", "service", "name", "description", "data", "labels", "created_at", "updated_at", "updated_by", "version", "owners", "changelog").
407-
Values(oldAsset.ID, oldAsset.URN, oldAsset.Type, oldAsset.Service, oldAsset.Name, oldAsset.Description, oldAsset.Data, oldAsset.Labels,
407+
Values(oldAsset.ID, oldAsset.URN, oldAsset.Types, oldAsset.Services, oldAsset.Name, oldAsset.Description, oldAsset.Data, oldAsset.Labels,
408408
oldAsset.CreatedAt, oldAsset.UpdatedAt, oldAsset.UpdatedBy.ID, oldAsset.Version, oldAsset.Owners, jsonChangelog).
409409
PlaceholderFormat(sq.Dollar).
410410
ToSql()
@@ -636,14 +636,14 @@ func (r *AssetRepository) getAssetVersionSQL() sq.SelectBuilder {
636636

637637
func (r *AssetRepository) buildConfigQuery(builder sq.SelectBuilder, cfg asset.Config) sq.SelectBuilder {
638638
clause := sq.Eq{}
639-
if len(cfg.Type) > 0 {
640-
builder = builder.Where("type @> ?", cfg.Type)
639+
if len(cfg.Types) > 0 {
640+
builder = builder.Where("type @> ?", cfg.Types)
641641
}
642642
//if cfg.Type != "" {
643643
// clause["type"] = cfg.Type
644644
//}
645-
if len(cfg.Service) > 0 {
646-
builder = builder.Where("service @> ?", cfg.Service)
645+
if len(cfg.Services) > 0 {
646+
builder = builder.Where("service @> ?", cfg.Services)
647647
}
648648
//if cfg.Service != "" {
649649
// clause["service"] = cfg.Service

0 commit comments

Comments
 (0)