uruki

package module
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 23, 2023 License: MIT Imports: 3 Imported by: 0

README

godoc

URUKI

URUKI (URi qUicK buIlder) uri / url parser wrapped with net/url. adjusted to safe mutate data with options of restricted scheme and automatically internal encode data. ref: https://en.wikipedia.org/wiki/Uniform_Resource_Identifier

Background using this wrapper:

  • quick update part of uri, because there is multi step if we using net/url to add / update / delete part of uri
  • keep consistent all uri parts (scheme, host, port, path, query, fragment)
  • get result of uri as string with automatically encoding
  • restrict space encode

Feature supports:

  • automatically encode by default setting
  • delete key query without force encode values
  • restricted scheme url, and encoding method
  • multi add key query param
  • set / get fragment
  • update base url with validation mechanism

Uruki Option

Option while initiate builder

Field Type Description
URL string URL existing to parse
RestrictScheme []string default no restrict scheme, for example if you want restrict scheme url only into http, https, and tokopedia. can use []string{"http", "https", "tokopedia"}
DefaultSpaceEncode SpaceEncoding space encoding method while escape query, refer to SpaceEncoding list below, default is keep space as is
UseEscapeAutomateURL bool automate escape existing query while init builder / SetURL(uri string), default false

SpaceEncoding method build in

  • WithoutEncoding = keep space as is
  • PercentTwentyEncoding = change space into %20
  • PlusEncoding = change space into +

Example Initiate

// without using options

ub, err := NewBuilder()
if err != nil {
    fmt.Println(err)
    return
}
got := ub.GetURLResult()

// got = ""
// using options without UseEscapeAutomateURL

ub, err := NewBuilder(Option{
    URL:                   "https://www.tokopedia.com/search?condition=1&fcity=174,175,176,177,178,179&navsource=&rf=true&rt=4,5&srp_component_id=02.01.00.00&srp_page_id=&srp_page_title=&st=product&q=macbook air m2",
    UseEscapeAutomateURL: false,
})
if err != nil {
    fmt.Println(err)
    return
}
got = ub.GetURLResult()

// got = "https://www.tokopedia.com/search?condition=1&fcity=174,175,176,177,178,179&navsource=&rf=true&rt=4,5&srp_component_id=02.01.00.00&srp_page_id=&srp_page_title=&st=product&q=macbook air m2"
// using options with UseEscapeAutomateURL and PercentTwentyEncoding

ub, err := NewBuilder(Option{
    URL:                   "https://www.tokopedia.com/search?condition=1&fcity=174,175,176,177,178,179&navsource=&rf=true&rt=4,5&srp_component_id=02.01.00.00&srp_page_id=&srp_page_title=&st=product&q=macbook air m2",
    UseEscapeAutomateURL: true,
    DefaultSpaceEncode:    PercentTwentyEncoding,
})
if err != nil {
    fmt.Println(err)
    return
}
got = ub.GetURLResult()

// got = "https://www.tokopedia.com/search?condition=1&fcity=174%2C175%2C176%2C177%2C178%2C179&navsource=&rf=true&rt=4%2C5&srp_component_id=02.01.00.00&srp_page_id=&srp_page_title=&st=product&q=macbook%20air%20m2"
// using options with UseEscapeAutomateURL, PercentTwentyEncoding and RestrictScheme
_, err = NewBuilder(Option{
    URL:                   "http://www.tokopedia.com/search?condition=1&fcity=174,175,176,177,178,179&navsource=&rf=true&rt=4,5&srp_component_id=02.01.00.00&srp_page_id=&srp_page_title=&st=product&q=macbook air m2",
    UseEscapeAutomateURL: true,
    DefaultSpaceEncode:    PercentTwentyEncoding,
    RestrictScheme:        []string{"https"},
})
// this will be error because we restrict scheme only https
if err != nil {
    fmt.Println(err)
    return
}
AddQueryParam

add query parameter values, internally will be encode the value of query, options param:

  • Key string = key query
  • Val string = value query
  • SpaceEnc string = specify space encode if you use custom encoding
  • UseDefaultEncode bool = if you want using DefaultSpaceEncoding as SpaceEnc
// using ampersand value and some without encoding space
ub, err := NewBuilder(Option{
    URL:                "https://tokopedia.com/search",
    DefaultSpaceEncode: PercentTwentyEncoding,
})
if err != nil {
    fmt.Println(err)
    return
}
err := ub.AddQueryParam(AddQueryParamOpt{Key:"q",Val:"produk p&g",SpaceEnc:PlusEncoding})
if err != nil {
    fmt.Println(err)
    return
}
url := ub.GetURLResult()
// url = "https://tokopedia.com/search?q=produk+p%26g"
SetBaseURL

change or update existing of base url only host and port

ub, err := NewBuilder(Option{
    URL:                "https://www.tokopedia.com/acmic/acmic-usb-c-to-lightning-adapter-iphone-converter-connector-konektor?extParam=ivf%3Dfalse%26src%3Dsearch%26whid%3D13355454",
    DefaultSpaceEncode: PercentTwentyEncoding,
    RestrictScheme:     []string{"https", "tokopedia"},
})
err = ub.SetBaseURL("tokopedia://")
if err != nil {
    fmt.Println(err)
    return
}
gotURL := ub.GetURLResult()
// url = "tokopedia://acmic/acmic-usb-c-to-lightning-adapter-iphone-converter-connector-konektor?extParam=ivf%3Dfalse%26src%3Dsearch%26whid%3D13355454"
SetPath

change or update path only of url

ub, err := NewBuilder(Option{
    URL:                "https://www.tokopedia.com/acmic/acmic-usb-c-to-lightning-adapter-iphone-converter-connector-konektor?extParam=ivf%3Dfalse%26src%3Dsearch%26whid%3D13355454",
    DefaultSpaceEncode: PercentTwentyEncoding,
})
if err != nil {
    fmt.Println(err)
    return
}
ub.SetPath("/iphoneos/acmic-konektor")
gotURL := ub.GetURLResult()
// url = "https://www.tokopedia.com/iphoneos/acmic-konektor?extParam=ivf%3Dfalse%26src%3Dsearch%26whid%3D13355454"
SetFragment

create / update existing fragment for references, without '#'. options:

  • Fragment string = fragment value
  • SpaceEnc string = specify space encode if you use custom encoding
  • UseDefaultEncode bool = if you want using DefaultSpaceEncoding as SpaceEnc
ub, err := NewBuilder(Option{
    URL:                "https://tokopedia.com/discovery",
    DefaultSpaceEncode: PercentTwentyEncoding,
})
if err != nil {
    fmt.Println(err)
    return
}
ub.SetFragment(SetFragmentOpt{Fragment: "top 10", SpaceEnc: PlusEncoding})
gotURL := ub.GetURLResult()
// url = "https://tokopedia.com/discovery#top+10"
DeleteKeyQuery

delete key query parameter if exist

ub, err := NewBuilder(Option{
    URL:                "https://www.tokopedia.com/search?st=product&q=produck%20p%26g&srp_component_id=01.07.00.00&srp_page_id=&srp_page_title=&navsource=&=exist_val_empty_key",
    DefaultSpaceEncode: PercentTwentyEncoding,
})
if err != nil {
    fmt.Println(err)
    return
}
ub.DeleteKeyQuery("st")
ub.DeleteKeyQuery("srp_page_id")
ub.DeleteKeyQuery("navsource")
url := ub.GetURLResult()
// url = "https://www.tokopedia.com/search?q=produck%20p%26g&srp_component_id=01.07.00.00&srp_page_title=&=exist_val_empty_key"
SetURL

replace all url with new url based on parameter, if error keep old url

ub, err := NewBuilder(Option{
    URL:                "https://www.tokopedia.com/acmic/acmic-usb-c-to-lightning-adapter-iphone-converter-connector-konektor?extParam=ivf%3Dfalse%26src%3Dsearch%26whid%3D13355454",
    DefaultSpaceEncode: PercentTwentyEncoding,
    RestrictScheme:     []string{"https", "tokopedia"},
})
if err != nil {
    fmt.Println(err)
    return
}
err = ub.SetURL("tokopedia://search?q=p+g&source=search#bottom")
if err != nil {
    fmt.Println(err)
    return
}
url := ub.GetURLResult()
// url = "tokopedia://search?q=p+g&source=search#bottom"

// expect error because scheme changes into http
err = ub.SetURL("http://www.tokopedia.com/now")
if err != nil {
    fmt.Println(err)
}
DeleteFragment

remove existing fragment if any

ub, err := NewBuilder(Option{
		URL:                "https://tokopedia.com/discovery#top",
		DefaultSpaceEncode: PercentTwentyEncoding,
	})
if err != nil {
    fmt.Println(err)
    return
}
ub.DeleteFragment()
url := ub.GetURLResult()
// url = "https://tokopedia.com/discovery"

Documentation

Index

Constants

View Source
const (
	WithoutEncoding       = " "   // Keep space as is
	PercentTwentyEncoding = "%20" // Change space into %20
	PlusEncoding          = "+"   // Change space into +

)

constants of SpaceEncoding

Variables

View Source
var (
	// ErrorInvalidSchemeURI error invalid scheme url not in restricted schemes
	ErrorInvalidSchemeURI = errors.New("invalid scheme url not in restricted schemes")
	// ErrorKeyEmpty key query parameter cannot be empty
	ErrorKeyEmpty = errors.New("key query parameter cannot be empty")
	// ErrorKeyContainSpace key query parameter cannot contains space
	ErrorKeyContainSpace = errors.New("key query parameter cannot contains space")
)
View Source
var (
	// ErrInvalidURL invalid url
	ErrInvalidURL = errors.New("invalid scheme url, it should https or http prefix")
)

Functions

This section is empty.

Types

type AddQueryParamOpt

type AddQueryParamOpt struct {
	// key query
	Key string
	// value query
	Val string
	// if you want using DefaultSpaceEncoding as SpaceEnc
	UseDefaultEncode bool
	// specify space encode if you use custom encoding
	SpaceEnc string
}

AddQueryParamOpt parameter for adding query params value in url

type Builder

type Builder struct {
	// contains filtered or unexported fields
}

Builder base struct uruki

func NewBuilder

func NewBuilder(options ...Option) (*Builder, error)

NewBuilder create uruki (URi qUicK buIlder) parser & wrapper of net/url

func (*Builder) AddQueryParam

func (ub *Builder) AddQueryParam(opt AddQueryParamOpt) error

AddQueryParam add query parameter values, internally will be encode the value of query

func (*Builder) DeleteFragment

func (ub *Builder) DeleteFragment()

DeleteFragment remove existing fragment if any

func (*Builder) DeleteKeyQuery

func (ub *Builder) DeleteKeyQuery(keyDelete string)

DeleteKeyQuery delete key query parameter if exist

func (*Builder) GetAllQueryValue

func (ub *Builder) GetAllQueryValue() map[string][]string

GetAllQueryValue get all key-value of existing query parameter, return as map key and decoded value

func (*Builder) GetFullPath

func (ub *Builder) GetFullPath() string

GetFullPath get full path. exclude base url, query parameter and fragment

func (*Builder) GetInternalURL

func (ub *Builder) GetInternalURL() url.URL

GetInternalURL get internal url that already parsed by uruki

func (*Builder) GetPaths

func (ub *Builder) GetPaths() []string

GetPaths get each part of path in slice

func (*Builder) GetURLResult

func (ub *Builder) GetURLResult() string

GetURLResult get url result with escaped option

func (*Builder) GetURLResultUnescape

func (ub *Builder) GetURLResultUnescape() string

GetURLResultUnescape get result url with unescape string

func (*Builder) GetValueQuery

func (ub *Builder) GetValueQuery(key string) string

GetValueQuery get value of existing query parameter if any, return as decoded value

func (*Builder) SetBaseURL

func (ub *Builder) SetBaseURL(baseURL string) error

SetBaseURL change or update existing of base url only host and port

func (*Builder) SetFragment

func (ub *Builder) SetFragment(opt SetFragmentOpt)

SetFragment create / update existing fragment for references, without '#'

func (*Builder) SetPath

func (ub *Builder) SetPath(path string)

SetPath change or update path only of url

func (*Builder) SetURL

func (ub *Builder) SetURL(uri string) error

SetURL replace all url with new url based on parameter, if error keep old url

type Option

type Option struct {
	// URL: url to proceed
	URL string
	// RestrictScheme: if you want your url scheme keep as is, example []string{"https"} if any url set with "http" options this will be return error
	RestrictScheme []string
	// DefaultSpaceEncode: default space encoding for internally encode. see SpaceEncoding const for more the details
	DefaultSpaceEncode string
	// UseEscapeAutomateURL to automate query escape on existing url query parameter while initiating builder / SetURL(uri string)
	UseEscapeAutomateURL bool
}

Option options to create new Builder

type SetFragmentOpt

type SetFragmentOpt struct {
	// fragment value
	Fragment string
	// if you want using DefaultSpaceEncoding as SpaceEnc
	UseDefaultEncode bool
	// specify space encode if you use custom encoding
	SpaceEnc string
}

SetFragmentOpt parameter for adding query params value in url

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL