-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
152 additions
and
74 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package interfaces | ||
|
||
import ( | ||
"github.com/crawlab-team/crawlab-db/mongo" | ||
"go.mongodb.org/mongo-driver/bson" | ||
"go.mongodb.org/mongo-driver/bson/primitive" | ||
) | ||
|
||
type GrpcClientModelEnvironmentService interface { | ||
ModelBaseService | ||
GetEnvironmentById(id primitive.ObjectID) (s Environment, err error) | ||
GetEnvironment(query bson.M, opts *mongo.FindOptions) (s Environment, err error) | ||
GetEnvironmentList(query bson.M, opts *mongo.FindOptions) (res []Environment, err error) | ||
} |
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,9 @@ | ||
package interfaces | ||
|
||
type Environment interface { | ||
Model | ||
GetKey() (key string) | ||
SetKey(key string) | ||
GetValue() (value string) | ||
SetValue(value string) | ||
} |
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,77 @@ | ||
package client | ||
|
||
import ( | ||
"github.com/crawlab-team/crawlab-core/errors" | ||
"github.com/crawlab-team/crawlab-core/interfaces" | ||
"github.com/crawlab-team/crawlab-db/mongo" | ||
"go.mongodb.org/mongo-driver/bson" | ||
"go.mongodb.org/mongo-driver/bson/primitive" | ||
) | ||
|
||
type EnvironmentServiceDelegate struct { | ||
interfaces.GrpcClientModelBaseService | ||
} | ||
|
||
func (svc *EnvironmentServiceDelegate) GetEnvironmentById(id primitive.ObjectID) (e interfaces.Environment, err error) { | ||
res, err := svc.GetById(id) | ||
if err != nil { | ||
return nil, err | ||
} | ||
s, ok := res.(interfaces.Environment) | ||
if !ok { | ||
return nil, errors.ErrorModelInvalidType | ||
} | ||
return s, nil | ||
} | ||
|
||
func (svc *EnvironmentServiceDelegate) GetEnvironment(query bson.M, opts *mongo.FindOptions) (e interfaces.Environment, err error) { | ||
res, err := svc.Get(query, opts) | ||
if err != nil { | ||
return nil, err | ||
} | ||
s, ok := res.(interfaces.Environment) | ||
if !ok { | ||
return nil, errors.ErrorModelInvalidType | ||
} | ||
return s, nil | ||
} | ||
|
||
func (svc *EnvironmentServiceDelegate) GetEnvironmentList(query bson.M, opts *mongo.FindOptions) (res []interfaces.Environment, err error) { | ||
list, err := svc.GetList(query, opts) | ||
if err != nil { | ||
return nil, err | ||
} | ||
for _, item := range list.GetModels() { | ||
s, ok := item.(interfaces.Environment) | ||
if !ok { | ||
return nil, errors.ErrorModelInvalidType | ||
} | ||
res = append(res, s) | ||
} | ||
return res, nil | ||
} | ||
|
||
func NewEnvironmentServiceDelegate(opts ...ModelBaseServiceDelegateOption) (svc2 interfaces.GrpcClientModelEnvironmentService, err error) { | ||
// apply options | ||
opts = append(opts, WithBaseServiceModelId(interfaces.ModelIdEnvironment)) | ||
|
||
// base service | ||
baseSvc, err := NewBaseServiceDelegate(opts...) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// service | ||
svc := &EnvironmentServiceDelegate{baseSvc} | ||
|
||
return svc, nil | ||
} | ||
|
||
func ProvideEnvironmentServiceDelegate(path string, opts ...ModelBaseServiceDelegateOption) func() (svc interfaces.GrpcClientModelEnvironmentService, err error) { | ||
if path != "" { | ||
opts = append(opts, WithBaseServiceConfigPath(path)) | ||
} | ||
return func() (svc interfaces.GrpcClientModelEnvironmentService, err error) { | ||
return NewEnvironmentServiceDelegate(opts...) | ||
} | ||
} |
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