Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/go-andiamo/urit
Go package for URI templates
https://github.com/go-andiamo/urit
go golang template templates uri uri-template uri-templates url url-template
Last synced: 11 days ago
JSON representation
Go package for URI templates
- Host: GitHub
- URL: https://github.com/go-andiamo/urit
- Owner: go-andiamo
- License: apache-2.0
- Created: 2022-11-03T18:57:28.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2023-07-19T18:26:23.000Z (over 1 year ago)
- Last Synced: 2024-07-30T20:58:56.477Z (3 months ago)
- Topics: go, golang, template, templates, uri, uri-template, uri-templates, url, url-template
- Language: Go
- Homepage:
- Size: 60.5 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Urit
[![GoDoc](https://godoc.org/github.com/go-andiamo/urit?status.svg)](https://pkg.go.dev/github.com/go-andiamo/urit)
[![Latest Version](https://img.shields.io/github/v/tag/go-andiamo/urit.svg?sort=semver&style=flat&label=version&color=blue)](https://github.com/go-andiamo/urit/releases)
[![codecov](https://codecov.io/gh/go-andiamo/urit/branch/main/graph/badge.svg?token=igjnZdgh0e)](https://codecov.io/gh/go-andiamo/urit)
[![Go Report Card](https://goreportcard.com/badge/github.com/go-andiamo/urit)](https://goreportcard.com/report/github.com/go-andiamo/urit)## Overview
Go package for templated URIs - enables building URIs from templates and extracting path vars from templates
Define path vars by name...
```go
template := urit.MustCreateTemplate(`/foo/{foo-id:[a-z]*}/bar/{bar-id:[0-9]*}`)
pth, _ := template.PathFrom(urit.Named(
"foo-id", "abc",
"bar-id", "123"))
println(pth)
```
or positional...
```go
template := urit.MustCreateTemplate(`/foo/?/bar/?`)
pth, _ := template.PathFrom(urit.Positional("abc", "123"))
println(pth)
```Extract vars from paths - using named...
```go
template := urit.MustCreateTemplate(`/credits/{year:[0-9]{4}}/{month:[0-9]{2}}`)
req, _ := http.NewRequest(`GET`, `http://www.example.com/credits/2022/11`, nil)
vars, ok := template.MatchesRequest(req)
println(ok)
println(vars.Get("year"))
println(vars.Get("month"))
```
Or extract using positional...
```go
template := urit.MustCreateTemplate(`/credits/?/?`)
req, _ := http.NewRequest(`GET`, `http://www.example.com/credits/2022/11`, nil)
vars, ok := template.MatchesRequest(req)
println(ok)
println(vars.Get(0))
println(vars.Get(1))
```Generate path from a template...
```go
template := urit.MustCreateTemplate(`/credits/{year:[0-9]{4}}/{month:[0-9]{2}}`)path, _ := template.PathFrom(urit.Named("year", "2022", "month", "11"))
println(path)
```## Installation
To install Urit, use go get:go get github.com/go-andiamo/urit
To update Urit to the latest version, run:
go get -u github.com/go-andiamo/urit