Skip to content
Snippets Groups Projects
Commit 4f0be128 authored by Pavel Antonov's avatar Pavel Antonov :asterisk:
Browse files

WIP: Изменена реализация action.URL

parent 57517bc5
No related branches found
No related tags found
No related merge requests found
package action package action
import ( import (
"fmt"
"net/url" "net/url"
"strings" "strings"
"git.perx.ru/perxis/perxis-go/pkg/errors"
) )
// URL структура для хранения данных о переданном действии. // URL структура для хранения данных о переданном действии.
type URL struct { type URL struct {
*url.URL *url.URL
id string
extension string
} }
// NewURL возвращает структуру ActionURL // NewURL возвращает структуру ActionURL
func NewURL(action string) (*URL, error) { func NewURL(action string) (*URL, error) {
actionURL := new(URL) u, err := url.Parse(action)
if action == "" {
return actionURL, nil
}
err := actionURL.SetURL(action)
if err != nil { if err != nil {
return nil, err return nil, err
} }
if actionURL.URL.Scheme == "grpc" { return &URL{URL: u}, nil
splitPath := strings.Split(strings.TrimLeft(actionURL.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) actionParts() (string, string) {
func (u *URL) SetID(id string) { if u.URL == nil && u.URL.Scheme == "grpc" {
u.id = id splitPath := strings.Split(strings.TrimLeft(u.Path, "/"), "/")
if len(splitPath) >= 2 {
return splitPath[0], splitPath[1]
} }
// Extension возвращает сохраненный в URL id расширения
func (u *URL) Extension() string {
return u.extension
} }
return "", ""
// SetExtension устанавливает в URL id расширения
func (u *URL) SetExtension(ext string) {
u.extension = ext
} }
// SetURL устанавливает структуру URL func (u *URL) Action() string {
func (u *URL) SetURL(s string) (err error) { _, action := u.actionParts()
if u.URL, err = url.Parse(s); err != nil { return action
return err
}
return nil
} }
// MakeURL возвращает Action URL из сохраненных данных func (u *URL) Extension() string {
func (u *URL) String() string { ext, _ := u.actionParts()
if u.id != "" && u.extension != "" { return ext
return fmt.Sprintf("grpc:///%s/%s", u.extension, u.id)
}
return u.URL.String()
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment