Skip to content

Commit 7a8f93b

Browse files
authored
chore: move search and namespace domain to search package (#139)
* chore: move search domain to search package * chore: move namespace domain to namespace
1 parent 490407c commit 7a8f93b

File tree

17 files changed

+113
-108
lines changed

17 files changed

+113
-108
lines changed

domain/namespace.go renamed to core/namespace/namespace.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package domain
1+
package namespace
22

33
import (
44
"context"

core/namespace/service.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,25 @@ package namespace
22

33
import (
44
"context"
5-
6-
"github.com/odpf/stencil/domain"
75
)
86

97
type Service struct {
10-
Repo domain.NamespaceRepository
8+
Repo NamespaceRepository
119
}
1210

13-
func (s Service) Create(ctx context.Context, ns domain.Namespace) (domain.Namespace, error) {
11+
func (s Service) Create(ctx context.Context, ns Namespace) (Namespace, error) {
1412
return s.Repo.CreateNamespace(ctx, ns)
1513
}
1614

17-
func (s Service) Update(ctx context.Context, ns domain.Namespace) (domain.Namespace, error) {
15+
func (s Service) Update(ctx context.Context, ns Namespace) (Namespace, error) {
1816
return s.Repo.UpdateNamespace(ctx, ns)
1917
}
2018

2119
func (s Service) List(ctx context.Context) ([]string, error) {
2220
return s.Repo.ListNamespaces(ctx)
2321
}
2422

25-
func (s Service) Get(ctx context.Context, name string) (domain.Namespace, error) {
23+
func (s Service) Get(ctx context.Context, name string) (Namespace, error) {
2624
return s.Repo.GetNamespace(ctx, name)
2725
}
2826

core/schema/service.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77

88
"github.com/google/uuid"
9+
"github.com/odpf/stencil/core/namespace"
910
"github.com/odpf/stencil/domain"
1011
"github.com/odpf/stencil/internal/store"
1112
)
@@ -28,7 +29,7 @@ func getBytes(key interface{}) []byte {
2829
return buf
2930
}
3031

31-
func NewService(repo domain.SchemaRepository, provider SchemaProvider, nsSvc domain.NamespaceService, cache schemaCache) *Service {
32+
func NewService(repo domain.SchemaRepository, provider SchemaProvider, nsSvc namespace.NamespaceService, cache schemaCache) *Service {
3233
return &Service{
3334
Repo: repo,
3435
SchemaProvider: provider,
@@ -45,7 +46,7 @@ type schemaCache interface {
4546
type Service struct {
4647
SchemaProvider SchemaProvider
4748
Repo domain.SchemaRepository
48-
NamespaceSvc domain.NamespaceService
49+
NamespaceSvc namespace.NamespaceService
4950
cache schemaCache
5051
}
5152

core/schema/service_test.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"errors"
66
"testing"
77

8+
"github.com/odpf/stencil/core/namespace"
89
"github.com/odpf/stencil/core/schema"
910
"github.com/odpf/stencil/domain"
1011
"github.com/odpf/stencil/internal/store"
@@ -30,7 +31,7 @@ func TestSchemaCreate(t *testing.T) {
3031
t.Run("should return error if namespace not found", func(t *testing.T) {
3132
svc, nsService, _, _ := getSvc()
3233
nsName := "testNamespace"
33-
nsService.On("Get", mock.Anything, nsName).Return(domain.Namespace{}, store.NoRowsErr)
34+
nsService.On("Get", mock.Anything, nsName).Return(namespace.Namespace{}, store.NoRowsErr)
3435
_, err := svc.Create(ctx, nsName, "a", &domain.Metadata{}, []byte(""))
3536
assert.NotNil(t, err)
3637
assert.ErrorIs(t, err, store.NoRowsErr)
@@ -41,7 +42,7 @@ func TestSchemaCreate(t *testing.T) {
4142
svc, nsService, schemaProvider, _ := getSvc()
4243
nsName := "testNamespace"
4344
data := []byte("data")
44-
nsService.On("Get", mock.Anything, nsName).Return(domain.Namespace{Format: "avro"}, nil)
45+
nsService.On("Get", mock.Anything, nsName).Return(namespace.Namespace{Format: "avro"}, nil)
4546
schemaProvider.On("ParseSchema", "protobuf", data).Return(&mocks.ParsedSchema{}, errors.New("invalid schema"))
4647
_, err := svc.Create(ctx, nsName, "a", &domain.Metadata{Format: "protobuf"}, data)
4748
assert.NotNil(t, err)
@@ -52,7 +53,7 @@ func TestSchemaCreate(t *testing.T) {
5253
svc, nsService, schemaProvider, _ := getSvc()
5354
nsName := "testNamespace"
5455
data := []byte("data")
55-
nsService.On("Get", mock.Anything, nsName).Return(domain.Namespace{Format: "protobuf"}, nil)
56+
nsService.On("Get", mock.Anything, nsName).Return(namespace.Namespace{Format: "protobuf"}, nil)
5657
schemaProvider.On("ParseSchema", "protobuf", data).Return(&mocks.ParsedSchema{}, errors.New("invalid schema"))
5758
_, err := svc.Create(ctx, nsName, "a", &domain.Metadata{}, data)
5859
assert.NotNil(t, err)
@@ -65,7 +66,7 @@ func TestSchemaCreate(t *testing.T) {
6566
parsedSchema := &mocks.ParsedSchema{}
6667
nsName := "testNamespace"
6768
data := []byte("data")
68-
nsService.On("Get", mock.Anything, nsName).Return(domain.Namespace{Format: "protobuf"}, nil)
69+
nsService.On("Get", mock.Anything, nsName).Return(namespace.Namespace{Format: "protobuf"}, nil)
6970
schemaProvider.On("ParseSchema", "protobuf", data).Return(parsedSchema, nil)
7071
schemaRepo.On("GetLatestVersion", mock.Anything, nsName, "a").Return(int32(2), store.NoRowsErr)
7172
parsedSchema.On("GetCanonicalValue").Return(scFile)
@@ -81,7 +82,7 @@ func TestSchemaCreate(t *testing.T) {
8182
parsedSchema := &mocks.ParsedSchema{}
8283
nsName := "testNamespace"
8384
data := []byte("data")
84-
nsService.On("Get", mock.Anything, nsName).Return(domain.Namespace{Format: "protobuf"}, nil)
85+
nsService.On("Get", mock.Anything, nsName).Return(namespace.Namespace{Format: "protobuf"}, nil)
8586
schemaProvider.On("ParseSchema", "protobuf", data).Return(parsedSchema, nil)
8687
schemaRepo.On("GetLatestVersion", mock.Anything, nsName, "a").Return(int32(2), errors.New("some other error apart from noRowsError"))
8788
_, err := svc.Create(ctx, nsName, "a", &domain.Metadata{}, data)
@@ -96,7 +97,7 @@ func TestSchemaCreate(t *testing.T) {
9697
nsName := "testNamespace"
9798
data := []byte("data aa")
9899
prevData := []byte("some prev data")
99-
nsService.On("Get", mock.Anything, nsName).Return(domain.Namespace{Format: "protobuf", Compatibility: "COMPATIBILITY_BACKWARD"}, nil)
100+
nsService.On("Get", mock.Anything, nsName).Return(namespace.Namespace{Format: "protobuf", Compatibility: "COMPATIBILITY_BACKWARD"}, nil)
100101
schemaProvider.On("ParseSchema", "protobuf", data).Return(parsedSchema, nil).Once()
101102
schemaRepo.On("GetSchemaMetadata", mock.Anything, nsName, "a").Return(&domain.Metadata{Format: "protobuf"}, nil)
102103
schemaRepo.On("GetLatestVersion", mock.Anything, nsName, "a").Return(int32(3), nil)
@@ -130,7 +131,7 @@ func TestSchemaCreate(t *testing.T) {
130131
if test.isError {
131132
compErr = errors.New("compatibilit error")
132133
}
133-
nsService.On("Get", mock.Anything, nsName).Return(domain.Namespace{Format: "protobuf", Compatibility: "COMPATIBILITY_BACKWARD"}, nil)
134+
nsService.On("Get", mock.Anything, nsName).Return(namespace.Namespace{Format: "protobuf", Compatibility: "COMPATIBILITY_BACKWARD"}, nil)
134135
schemaProvider.On("ParseSchema", "protobuf", data).Return(parsedSchema, nil).Once()
135136
schemaRepo.On("GetSchemaMetadata", mock.Anything, nsName, "a").Return(&domain.Metadata{Format: "protobuf"}, nil)
136137
schemaRepo.On("GetLatestVersion", mock.Anything, nsName, "a").Return(int32(3), nil)

domain/search.go renamed to core/search/search.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package domain
1+
package search
22

33
import "context"
44

core/search/service.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ package search
33
import (
44
"context"
55
"errors"
6-
7-
"github.com/odpf/stencil/domain"
86
)
97

108
var (
@@ -14,10 +12,10 @@ var (
1412
)
1513

1614
type Service struct {
17-
Repo domain.SearchRepository
15+
Repo SearchRepository
1816
}
1917

20-
func (s *Service) Search(ctx context.Context, req *domain.SearchRequest) (*domain.SearchResponse, error) {
18+
func (s *Service) Search(ctx context.Context, req *SearchRequest) (*SearchResponse, error) {
2119
if req.Query == "" {
2220
return nil, ErrEmptyQueryString
2321
}
@@ -26,7 +24,7 @@ func (s *Service) Search(ctx context.Context, req *domain.SearchRequest) (*domai
2624
return nil, ErrEmptyNamespaceID
2725
}
2826

29-
var res []*domain.SearchHits
27+
var res []*SearchHits
3028
var err error
3129
if req.VersionID == 0 && !req.History {
3230
res, err = s.Repo.SearchLatest(ctx, req)
@@ -40,7 +38,7 @@ func (s *Service) Search(ctx context.Context, req *domain.SearchRequest) (*domai
4038
if err != nil {
4139
return nil, err
4240
}
43-
return &domain.SearchResponse{
41+
return &SearchResponse{
4442
Hits: res,
4543
}, nil
4644
}

internal/api/api.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77

88
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
99
"github.com/newrelic/go-agent/v3/newrelic"
10+
"github.com/odpf/stencil/core/namespace"
11+
"github.com/odpf/stencil/core/search"
1012
"github.com/odpf/stencil/domain"
1113
stencilv1beta1 "github.com/odpf/stencil/proto/odpf/stencil/v1beta1"
1214
"google.golang.org/grpc/health/grpc_health_v1"
@@ -19,12 +21,12 @@ type errHandleFunc func(http.ResponseWriter, *http.Request, map[string]string) e
1921
type API struct {
2022
stencilv1beta1.UnimplementedStencilServiceServer
2123
grpc_health_v1.UnimplementedHealthServer
22-
namespace domain.NamespaceService
24+
namespace namespace.NamespaceService
2325
schema domain.SchemaService
24-
search domain.SearchService
26+
search search.SearchService
2527
}
2628

27-
func NewAPI(namespace domain.NamespaceService, schema domain.SchemaService, search domain.SearchService) *API {
29+
func NewAPI(namespace namespace.NamespaceService, schema domain.SchemaService, search search.SearchService) *API {
2830
return &API{
2931
namespace: namespace,
3032
schema: schema,

internal/api/namespace.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,21 @@ package api
33
import (
44
"context"
55

6-
"github.com/odpf/stencil/domain"
6+
"github.com/odpf/stencil/core/namespace"
77
stencilv1beta1 "github.com/odpf/stencil/proto/odpf/stencil/v1beta1"
88
"google.golang.org/protobuf/types/known/timestamppb"
99
)
1010

11-
func createNamespaceRequestToNamespace(r *stencilv1beta1.CreateNamespaceRequest) domain.Namespace {
12-
return domain.Namespace{
11+
func createNamespaceRequestToNamespace(r *stencilv1beta1.CreateNamespaceRequest) namespace.Namespace {
12+
return namespace.Namespace{
1313
ID: r.GetId(),
1414
Format: r.GetFormat().String(),
1515
Compatibility: r.GetCompatibility().String(),
1616
Description: r.GetDescription(),
1717
}
1818
}
1919

20-
func namespaceToProto(ns domain.Namespace) *stencilv1beta1.Namespace {
20+
func namespaceToProto(ns namespace.Namespace) *stencilv1beta1.Namespace {
2121
return &stencilv1beta1.Namespace{
2222
Id: ns.ID,
2323
Format: stencilv1beta1.Schema_Format(stencilv1beta1.Schema_Format_value[ns.Format]),
@@ -36,7 +36,7 @@ func (a *API) CreateNamespace(ctx context.Context, in *stencilv1beta1.CreateName
3636
}
3737

3838
func (a *API) UpdateNamespace(ctx context.Context, in *stencilv1beta1.UpdateNamespaceRequest) (*stencilv1beta1.UpdateNamespaceResponse, error) {
39-
ns, err := a.namespace.Update(ctx, domain.Namespace{ID: in.GetId(), Format: in.GetFormat().String(), Compatibility: in.GetCompatibility().String(), Description: in.GetDescription()})
39+
ns, err := a.namespace.Update(ctx, namespace.Namespace{ID: in.GetId(), Format: in.GetFormat().String(), Compatibility: in.GetCompatibility().String(), Description: in.GetDescription()})
4040
return &stencilv1beta1.UpdateNamespaceResponse{Namespace: namespaceToProto(ns)}, err
4141
}
4242

internal/api/search.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ import (
44
"context"
55
"fmt"
66

7-
"github.com/odpf/stencil/domain"
7+
"github.com/odpf/stencil/core/search"
88
stencilv1beta1 "github.com/odpf/stencil/proto/odpf/stencil/v1beta1"
99
)
1010

1111
func (a *API) Search(ctx context.Context, in *stencilv1beta1.SearchRequest) (*stencilv1beta1.SearchResponse, error) {
12-
searchReq := &domain.SearchRequest{
12+
searchReq := &search.SearchRequest{
1313
NamespaceID: in.GetNamespaceId(),
1414
Query: in.GetQuery(),
1515
SchemaID: in.GetSchemaId(),

internal/store/postgres/repository.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"github.com/georgysavva/scany/pgxscan"
99
"github.com/jackc/pgconn"
1010
"github.com/jackc/pgx/v4"
11+
"github.com/odpf/stencil/core/namespace"
12+
"github.com/odpf/stencil/core/search"
1113
"github.com/odpf/stencil/domain"
1214
"github.com/odpf/stencil/internal/store"
1315
)
@@ -42,20 +44,20 @@ func (r *Store) Close() {
4244
r.db.Close()
4345
}
4446

45-
func (r *Store) CreateNamespace(ctx context.Context, ns domain.Namespace) (domain.Namespace, error) {
46-
newNamespace := domain.Namespace{}
47+
func (r *Store) CreateNamespace(ctx context.Context, ns namespace.Namespace) (namespace.Namespace, error) {
48+
newNamespace := namespace.Namespace{}
4749
err := pgxscan.Get(ctx, r.db, &newNamespace, namespaceInsertQuery, ns.ID, ns.Format, ns.Compatibility, ns.Description)
4850
return newNamespace, wrapError(err, ns.ID)
4951
}
5052

51-
func (r *Store) UpdateNamespace(ctx context.Context, ns domain.Namespace) (domain.Namespace, error) {
52-
newNamespace := domain.Namespace{}
53+
func (r *Store) UpdateNamespace(ctx context.Context, ns namespace.Namespace) (namespace.Namespace, error) {
54+
newNamespace := namespace.Namespace{}
5355
err := pgxscan.Get(ctx, r.db, &newNamespace, namespaceUpdateQuery, ns.ID, ns.Format, ns.Compatibility, ns.Description)
5456
return newNamespace, wrapError(err, ns.ID)
5557
}
5658

57-
func (r *Store) GetNamespace(ctx context.Context, id string) (domain.Namespace, error) {
58-
newNamespace := domain.Namespace{}
59+
func (r *Store) GetNamespace(ctx context.Context, id string) (namespace.Namespace, error) {
60+
newNamespace := namespace.Namespace{}
5961
err := pgxscan.Get(ctx, r.db, &newNamespace, namespaceGetQuery, id)
6062
return newNamespace, wrapError(err, id)
6163
}
@@ -151,14 +153,14 @@ func (r *Store) DeleteVersion(ctx context.Context, ns string, sc string, version
151153
return wrapError(err, "delete version")
152154
}
153155

154-
func (r *Store) Search(ctx context.Context, req *domain.SearchRequest) ([]*domain.SearchHits, error) {
155-
var searchHits []*domain.SearchHits
156+
func (r *Store) Search(ctx context.Context, req *search.SearchRequest) ([]*search.SearchHits, error) {
157+
var searchHits []*search.SearchHits
156158
err := pgxscan.Select(ctx, r.db, &searchHits, searchAllQuery, req.NamespaceID, req.SchemaID, req.VersionID, req.Query)
157159
return searchHits, err
158160
}
159161

160-
func (r *Store) SearchLatest(ctx context.Context, req *domain.SearchRequest) ([]*domain.SearchHits, error) {
161-
var searchHits []*domain.SearchHits
162+
func (r *Store) SearchLatest(ctx context.Context, req *search.SearchRequest) ([]*search.SearchHits, error) {
163+
var searchHits []*search.SearchHits
162164
err := pgxscan.Select(ctx, r.db, &searchHits, searchLatestQuery, req.NamespaceID, req.SchemaID, req.Query)
163165
return searchHits, err
164166
}

0 commit comments

Comments
 (0)