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