diff --git a/pkg/action/action.go b/pkg/action/action.go new file mode 100644 index 0000000000000000000000000000000000000000..0993fcf2867ac9e85b56a5bae8c943e4dedce6ae --- /dev/null +++ b/pkg/action/action.go @@ -0,0 +1,80 @@ +package action + +import ( + "fmt" + "net/url" + "strings" + + "git.perx.ru/perxis/perxis-go/pkg/errors" +) + +// Url структура для хранения данных о переданном действии. +type Url struct { + id string + extension string + *url.URL +} + +// NewUrl возвращает структуру ActionURL +func NewUrl(action string) (*Url, error) { + actionURL := &Url{} + + if action == "" { + return actionURL, nil + } + + err := actionURL.SetURL(action) + if err != nil { + return nil, err + } + if actionURL.URL.Scheme == "grpc" { + path := actionURL.Path + if strings.HasPrefix(actionURL.Path, "/") { + path = actionURL.Path[1:] + } + splitPath := strings.Split(path, "/") + if len(splitPath) < 2 { + return nil, errors.Errorf("incorrect action URL, no action id: '%s'", action) + } + actionURL.extension = splitPath[0] + actionURL.id = splitPath[1] + } + + return actionURL, nil +} + +// ID возвращает сохраненный в Url id действия +func (u *Url) ID() string { + return u.id +} + +// SetID устанавливает в Url id действия +func (u *Url) SetID(id string) { + u.id = id +} + +// Extension возвращает сохраненный в Url id расширения +func (u *Url) Extension() string { + return u.extension +} + +// SetExtension устанавливает в Url id расширения +func (u *Url) SetExtension(ext string) { + u.extension = ext +} + +// SetURL устанавливает структуру URL +func (u *Url) SetURL(s string) (err error) { + if u.URL, err = url.Parse(s); err != nil { + return err + } + return nil +} + +// Make возвращает Action URL из сохраненных данных +func (u *Url) Make() string { + if u.id != "" && u.extension != "" { + return fmt.Sprintf("grpc:///%s/%s", u.extension, u.id) + } + return u.URL.String() +} diff --git a/pkg/actionurl/actionurl_test.go b/pkg/action/action_test.go similarity index 88% rename from pkg/actionurl/actionurl_test.go rename to pkg/action/action_test.go index 72e8d16273a29de0d125be1c75eb6bc40d3e1518..e511ed454e7ab3b7cc62669821c6cde25be072e8 100644 --- a/pkg/actionurl/actionurl_test.go +++ b/pkg/action/action_test.go @@ -1,4 +1,4 @@ -package actionurl +package action import ( "fmt" @@ -12,26 +12,26 @@ func TestActionURL_New(t *testing.T) { tests := []struct { name string action string - want *ActionURL + want *Url url string wantErr assert.ErrorAssertionFunc }{ { name: "Without action", - want: &ActionURL{}, + want: &Url{}, wantErr: assert.NoError, }, { name: "Without deprecated action call", action: "build-site", - want: &ActionURL{}, + want: &Url{}, url: "build-site", wantErr: assert.NoError, }, { name: "With grpc action", action: "grpc:///perxisweb/build-site", - want: &ActionURL{ + want: &Url{ id: "build-site", extension: "perxisweb", }, @@ -41,7 +41,7 @@ func TestActionURL_New(t *testing.T) { { name: "With ui action", action: "ui:///space/env/coll", - want: &ActionURL{ + want: &Url{ id: "", extension: "", }, @@ -51,7 +51,7 @@ func TestActionURL_New(t *testing.T) { { name: "With http action", action: "https://perx.ru", - want: &ActionURL{}, + want: &Url{}, url: "https://perx.ru", wantErr: assert.NoError, }, @@ -73,11 +73,11 @@ func TestActionURL_New(t *testing.T) { if tt.url != "" { tt.want.URL, _ = url.Parse(tt.url) } - got, err := New(tt.action) - if !tt.wantErr(t, err, fmt.Sprintf("New(%v)", tt.action)) { + got, err := NewUrl(tt.action) + if !tt.wantErr(t, err, fmt.Sprintf("NewUrl(%v)", tt.action)) { return } - assert.Equalf(t, tt.want, got, "New(%v)", tt.action) + assert.Equalf(t, tt.want, got, "NewUrl(%v)", tt.action) }) } } @@ -119,7 +119,7 @@ func TestActionURL_Make(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - p := &ActionURL{ + p := &Url{ id: tt.id, extension: tt.extension, } diff --git a/pkg/actionurl/action_url.go b/pkg/actionurl/action_url.go deleted file mode 100644 index fc5c8b029f66adbb0c631e43e4e380d3dc8212ea..0000000000000000000000000000000000000000 --- a/pkg/actionurl/action_url.go +++ /dev/null @@ -1,80 +0,0 @@ -package actionurl - -import ( - "fmt" - "net/url" - "strings" - - "git.perx.ru/perxis/perxis-go/pkg/errors" -) - -// ActionURL структура для хранения данных о переданном действии. -type ActionURL struct { - id string - extension string - *url.URL -} - -// New возвращает структуру ActionURL -func New(action string) (*ActionURL, error) { - actionURL := &ActionURL{} - - if action == "" { - return actionURL, nil - } - - err := actionURL.SetURL(action) - if err != nil { - return nil, err - } - if actionURL.URL.Scheme == "grpc" { - path := actionURL.Path - if strings.HasPrefix(actionURL.Path, "/") { - path = actionURL.Path[1:] - } - splitPath := strings.Split(path, "/") - if len(splitPath) < 2 { - return nil, errors.Errorf("incorrect action URL, no action id: '%s'", action) - } - actionURL.extension = splitPath[0] - actionURL.id = splitPath[1] - } - - return actionURL, nil -} - -// ID возвращает сохраненный в ActionURL id действия -func (p *ActionURL) ID() string { - return p.id -} - -// SetID устанавливает в ActionURL id действия -func (p *ActionURL) SetID(id string) { - p.id = id -} - -// Extension возвращает сохраненный в ActionURL id расширения -func (p *ActionURL) Extension() string { - return p.extension -} - -// SetExtension устанавливает в ActionURL id расширения -func (p *ActionURL) SetExtension(ext string) { - p.extension = ext -} - -// SetURL устанавливает структуру URL -func (p *ActionURL) SetURL(u string) (err error) { - if p.URL, err = url.Parse(u); err != nil { - return err - } - return nil -} - -// Make возвращает Action URL из сохраненных данных -func (p *ActionURL) Make() string { - if p.id != "" && p.extension != "" { - return fmt.Sprintf("grpc:///%s/%s", p.extension, p.id) - } - return p.URL.String() -} diff --git a/pkg/extension/server.go b/pkg/extension/server.go index 156997c53d11c3d8c5a23b4ac8e39c5342105275..c47f1ac2fd4843e5f8ebde38e3bc8147fcc8b8dd 100644 --- a/pkg/extension/server.go +++ b/pkg/extension/server.go @@ -3,7 +3,7 @@ package extension import ( "context" - "git.perx.ru/perxis/perxis-go/pkg/actionurl" + "git.perx.ru/perxis/perxis-go/pkg/action" "git.perx.ru/perxis/perxis-go/pkg/errors" pb "git.perx.ru/perxis/perxis-go/proto/extensions" ) @@ -83,7 +83,7 @@ func (srv *Server) Update(ctx context.Context, request *UpdateRequest) (*UpdateR func (srv *Server) Action(ctx context.Context, in *pb.ActionRequest) (*pb.ActionResponse, error) { ext := in.Extension if ext == "" { - actionURL, err := actionurl.New(in.Action) + actionURL, err := action.NewUrl(in.Action) if err != nil { return nil, err } diff --git a/pkg/extension/service/extension.go b/pkg/extension/service/extension.go index 517464347e496cb17f084225b0e4e8883dbdb28f..b670f7c703466cc77cd02e6023bf53493395a8d4 100644 --- a/pkg/extension/service/extension.go +++ b/pkg/extension/service/extension.go @@ -4,7 +4,7 @@ import ( "context" "fmt" - "git.perx.ru/perxis/perxis-go/pkg/actionurl" + "git.perx.ru/perxis/perxis-go/pkg/action" "git.perx.ru/perxis/perxis-go/pkg/clients" "git.perx.ru/perxis/perxis-go/pkg/content" "git.perx.ru/perxis/perxis-go/pkg/errors" @@ -158,7 +158,7 @@ func (s *Extension) Uninstall(ctx context.Context, in *extension.UninstallReques func (s *Extension) Action(ctx context.Context, in *extension.ActionRequest) (*extension.ActionResponse, error) { ext := in.Extension if ext == "" { - actionURL, err := actionurl.New(in.Action) + actionURL, err := action.NewUrl(in.Action) if err != nil { return nil, err }