From 0ca3f2f7e475101ba31fbdd2446406f8982c5aad Mon Sep 17 00:00:00 2001 From: ko_oler <kooler89@gmail.com> Date: Fri, 29 Sep 2023 15:19:13 +0300 Subject: [PATCH] =?UTF-8?q?=D0=BF=D1=80=D0=B0=D0=B2=D0=BA=D0=B8=20=D0=BF?= =?UTF-8?q?=D0=BE=20=D0=9F=D0=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/action/action.go | 30 ++++------- pkg/action/action_test.go | 111 ++++++++++++++++++++++++-------------- 2 files changed, 81 insertions(+), 60 deletions(-) diff --git a/pkg/action/action.go b/pkg/action/action.go index b1aabeb8..730f222f 100644 --- a/pkg/action/action.go +++ b/pkg/action/action.go @@ -1,7 +1,6 @@ package action import ( - "fmt" "net/url" "strings" @@ -11,9 +10,6 @@ import ( // URL структура для хранения данных о переданном действии. type URL struct { *url.URL - - id string - extension string } // NewURL возвращает структуру ActionURL @@ -24,7 +20,8 @@ func NewURL(action string) (*URL, error) { return actionURL, nil } - err := actionURL.SetURL(action) + var err error + actionURL.URL, err = url.Parse(action) if err != nil { return nil, err } @@ -33,8 +30,6 @@ func NewURL(action string) (*URL, error) { 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 @@ -42,36 +37,29 @@ func NewURL(action string) (*URL, error) { // ID возвращает сохраненный в URL id действия func (u *URL) ID() string { - return u.id + return strings.Split(strings.TrimLeft(u.Path, "/"), "/")[1] } // SetID устанавливает в URL id действия func (u *URL) SetID(id string) { - u.id = id + if u.Scheme == "grpc" { + u.Path = "/" + u.Extension() + "/" + id + } } // Extension возвращает сохраненный в URL id расширения func (u *URL) Extension() string { - return u.extension + return strings.Split(strings.TrimLeft(u.Path, "/"), "/")[0] } // 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 + if u.Scheme == "grpc" { + u.Path = "/" + ext + "/" + u.ID() } - return nil } // MakeURL возвращает Action URL из сохраненных данных func (u *URL) String() string { - if u.id != "" && u.extension != "" { - return fmt.Sprintf("grpc:///%s/%s", u.extension, u.id) - } return u.URL.String() } diff --git a/pkg/action/action_test.go b/pkg/action/action_test.go index 8f5eb933..f9d4d306 100644 --- a/pkg/action/action_test.go +++ b/pkg/action/action_test.go @@ -22,9 +22,23 @@ func TestActionURL_New(t *testing.T) { wantErr: assert.NoError, }, { - name: "Without deprecated action call", - action: "build-site", - want: &URL{}, + name: "Without deprecated action call", + action: "build-site", + want: &URL{ + URL: &url.URL{ + Scheme: "", + Opaque: "", + User: nil, + Host: "", + Path: "build-site", + RawPath: "", + OmitHost: false, + ForceQuery: false, + RawQuery: "", + Fragment: "", + RawFragment: "", + }, + }, url: "build-site", wantErr: assert.NoError, }, @@ -32,8 +46,19 @@ func TestActionURL_New(t *testing.T) { name: "With grpc action", action: "grpc:///perxisweb/build-site", want: &URL{ - id: "build-site", - extension: "perxisweb", + URL: &url.URL{ + Scheme: "grpc", + Opaque: "", + User: nil, + Host: "", + Path: "/perxisweb/build-site", + RawPath: "", + OmitHost: false, + ForceQuery: false, + RawQuery: "", + Fragment: "", + RawFragment: "", + }, }, url: "grpc:///perxisweb/build-site", wantErr: assert.NoError, @@ -42,16 +67,41 @@ func TestActionURL_New(t *testing.T) { name: "With ui action", action: "ui:///space/env/coll", want: &URL{ - id: "", - extension: "", + URL: &url.URL{ + Scheme: "ui", + Opaque: "", + User: nil, + Host: "", + Path: "/space/env/coll", + RawPath: "", + OmitHost: false, + ForceQuery: false, + RawQuery: "", + Fragment: "", + RawFragment: "", + }, }, url: "ui:///space/env/coll", wantErr: assert.NoError, }, { - name: "With http action", - action: "https://perx.ru", - want: &URL{}, + name: "With http action", + action: "https://perx.ru", + want: &URL{ + URL: &url.URL{ + Scheme: "https", + Opaque: "", + User: nil, + Host: "perx.ru", + Path: "", + RawPath: "", + OmitHost: false, + ForceQuery: false, + RawQuery: "", + Fragment: "", + RawFragment: "", + }, + }, url: "https://perx.ru", wantErr: assert.NoError, }, @@ -70,9 +120,6 @@ func TestActionURL_New(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if tt.url != "" { - tt.want.URL, _ = url.Parse(tt.url) - } got, err := NewURL(tt.action) if !tt.wantErr(t, err, fmt.Sprintf("NewURL(%v)", tt.action)) { return @@ -82,51 +129,37 @@ func TestActionURL_New(t *testing.T) { } } -func TestActionURL_Make(t *testing.T) { +func TestActionURL_String(t *testing.T) { tests := []struct { - name string - id string - extension string - url string - want string + name string + url string + want string }{ { - name: "Without action and extensions id's #1", - url: "grpc:///extension-id/action-id", - want: "grpc:///extension-id/action-id", - }, - { - name: "Without action and extensions id's #2", + name: "UI action #1", url: "ui:///space/env/coll", want: "ui:///space/env/coll", }, { - name: "Without action and extensions id's #3", + name: "UI action deprecated call #2", url: "space/env/coll", want: "space/env/coll", }, { - name: "Without action and extensions id's #4", + name: "Https action", url: "https://perx.ru", want: "https://perx.ru", }, { - name: "With action and extensions", - id: "action-id", - extension: "extension-id", - want: "grpc:///extension-id/action-id", + name: "With action deprecated call", + url: "extension-id", + want: "extension-id", }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - p := &URL{ - id: tt.id, - extension: tt.extension, - } - if tt.url != "" { - p.URL, _ = url.Parse(tt.url) - } - assert.Equalf(t, tt.want, p.MakeURL(), "MakeFromURL()") + p, _ := NewURL(tt.url) + assert.Equalf(t, tt.want, p.String(), "String()") }) } } -- GitLab