URI Templates (RFC 6570) implemented in Go

Related tags

Network uritemplates
Overview

uritemplates

-- import "github.com/jtacoma/uritemplates"

Build Status Coverage Status

Package uritemplates is a level 4 implementation of RFC 6570 (URI Template, http://tools.ietf.org/html/rfc6570).

To use uritemplates, parse a template string and expand it with a value map:

template, _ := uritemplates.Parse("https://api.github.com/repos{/user,repo}")
values := make(map[string]interface{})
values["user"] = "jtacoma"
values["repo"] = "uritemplates"
expanded, _ := template.Expand(values)
fmt.Printf(expanded)

License

Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.

Issues
  • passing values as a struct

    passing values as a struct

    I've been toying with the idea of using uritemplates for go-github for a little while now. In particular, I'm interested in trying to clean up some of the code I have for appending query parameters, that currently looks something like:

    type ListOptions struct {
            Page int
    }
    
    func (s *OrganizationsService) List(user string, opt *ListOptions) ([]Organization, error) {
            u := fmt.Sprintf("users/%v/orgs", user)
            if opt != nil {
                    params := url.Values{
                            "page": []string{strconv.Itoa(opt.Page)},
                    }
                    u += "?" + params.Encode()
            }
            ...
    }
    

    For small option types this isn't too bad, but gets worse with larger ones and just seems like it could be cleaner. What I've been thinking about is mimicking how encoding/json and encoding/xml use field tags. So something like:

    type ListOptions struct {
            Page int `url:"page"`
    }
    
    func (s *OrganizationsService) List(user string, opt *ListOptions) ([]Organization, error) {
            u := fmt.Sprintf("users/%v/orgs", user)
            u += uritemplates.Parse("{?page}").Expand(ListOptions)
            ...
    }
    

    Of course, I would ideally just have a single line with a template of users/{user}/orgs{?page}, but I haven't decided on the best way to pass the user into Expand() yet. I don't want to put it in ListOptions since it's a required parameter to the List() function and I want that to be explicit.

    There are of course ways to do this by converting the struct into a map first, and then passing that to Expand(), but I figured I'd float this idea to you and see what you thought.

    opened by willnorris 3
  • Feature request: matching and reverse

    Feature request: matching and reverse

    Its would be handy if we can match and extract value from an url, for example:

    template, _ := uritemplates.Parse("api.github.com/repos{/user,repo}") agrs := make(map[string]interface{}) if template.Match("api.github.com/repos/jtacoma/uritemplates", args) { fmt.Println(map["user"]) // jtacoma fmt.Println(map["repo"]) // uritemplates }

    opened by nvcnvn 3
  • Please tag version

    Please tag version

    I'm currently packaging your library in Debian, as a dependency of several other packages.

    You currently do not tag any recent releases, and this makes it harder for downstream packagers. (Also, referring to specific commits is awkward)

    opened by nbraud 2
  • ExpressionNames(): return expressions used in template

    ExpressionNames(): return expressions used in template

    I've got a project that uses URI templates from external sources, and before expanding, I'd like to ensure all of the prerequisite data is available. So I've added the ExpressionNames function to URITemplate that will return the list of names referenced by the template.

    I may be overlooking a corner case; if there are additional tests needed, or if they should be broken out into their own test instead of just piggybacking on Expand's tests, I'm happy to do that.

    opened by pd 2
  • Clarify license

    Clarify license

    / Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file.

    Do you consider MIT a BSD-style license? Or was there another license before?

    opened by aviau 1
  • Use bytes.Buffer instead of concatenation.

    Use bytes.Buffer instead of concatenation.

    This seemed like a promising improvement on paper but benchmarks currently included in the uritemplates_test.go show no significant differences so I'm submitting this pull request as a request for comments.

    opened by jtacoma 0
  • hyphen (

    hyphen ("-") in named path params is treated invalid

    uritemplates.Parse() gives error if the path has hyphen in one of the path-param name.

    Example : template, parseErr := uritemplates.Parse("https://api.github.com/repos/{repo-name}")

    Gives error : not a valid name: repo-name

    I am not able to comprehend why shouldn't this be allowed ? the regex used for validation is -----> "^([A-Za-z0-9_\.]|%[0-9A-Fa-f][0-9A-Fa-f])+$"

    opened by PankhudiB 1
  • Handle explicit nil value

    Handle explicit nil value

    Currently this code handles missing keys correctly, but a key that exists and has been explicitly set to nil causes a panic. This handles it the same way as a key that doesn't exist.

    opened by packrat386 0
Owner
Joshua Tacoma
Joshua Tacoma
Package arp implements the ARP protocol, as described in RFC 826. MIT Licensed.

arp Package arp implements the ARP protocol, as described in RFC 826. MIT Licensed. Portions of this code are taken from the Go standard library. The

Matt Layher 289 Jul 27, 2022
CoAP Client/Server implementing RFC 7252 for the Go Language

Canopus Canopus is a client/server implementation of the Constrained Application Protocol (CoAP) Updates 25.11.2016 I've added basic dTLS Support base

Zubair Hamed 148 Jan 23, 2022
Package dhcp6 implements a DHCPv6 server, as described in RFC 3315. MIT Licensed.

dhcp6 Package dhcp6 implements a DHCPv6 server, as described in IETF RFC 3315. MIT Licensed. At this time, the API is not stable, and may change over

Matt Layher 74 May 8, 2022
Fast RFC 5389 STUN implementation in go

STUN Package stun implements Session Traversal Utilities for NAT (STUN) [RFC5389] protocol and client with no external dependencies and zero allocatio

null 489 Jul 15, 2022
Diameter stack and Base Protocol (RFC 6733) for the Go programming language

Diameter Base Protocol Package go-diameter is an implementation of the Diameter Base Protocol RFC 6733 and a stack for the Go programming language. St

Alexandre Fiori 204 Jul 27, 2022
Fast RFC 5389 STUN implementation in go

STUN Package stun implements Session Traversal Utilities for NAT (STUN) [RFC5389] protocol and client with no external dependencies and zero allocatio

null 489 Jul 15, 2022
RFC 1413 compliant fake identd

RFC 1413 compliant fake identd. It is an implementation of the Ident Protocol, but it lies to the clients and always returns fake identities of queried users.

Hyeon Kim (김지현) 2 Jan 30, 2022
Rabbitio - Rabbit stream cipher package RFC 4503 for Go

rabbitio rabbitio is a rabbit stream cipher packge based on RFC 4503 for golang

Sina Ghaderi 7 Feb 8, 2022
Capture packet request/response pairs for a port and/or IP to aid in Network protocol based Nuclei Templates creation.

network-fingerprint Capture packet request/response pairs for a port and/or IP to aid in Network protocol based Nuclei Templates creation. Resources I

ProjectDiscovery 46 Jul 25, 2022
x-crafter is used to quickly create templates from your prototype, also come with a builder to quickly regenerate your code

XCrafter ?? x-crafter is used to quickly create templates from your prototype, also come with a builder to quickly regenerate your code. Install Using

Chi-Tai Vong 3 Nov 29, 2021
BGP implemented in the Go Programming Language

GoBGP: BGP implementation in Go GoBGP is an open source BGP implementation designed from scratch for modern environment and implemented in a modern pr

null 2.9k Jul 30, 2022
A persistent queue implemented in Go.

goq goq is a persistent queue implemented in Go. Features Communication over HTTP RESTful with JSON responses (trivial to write client libraries) Mini

Kunal Anand 29 Feb 10, 2022
communicate with iOS devices implemented with Golang

Golang-iDevice much more easy to use ?? electricbubble/gidevice-cli Installation go get github.com/electricbubble/gidevice Devices package main impor

雷系泡泡 191 Aug 2, 2022
A TCP socket based chat server implemented using Go

Go Chat Server A better TCP socket chat server implemented using Go Connecting nc localhost 5000 Docker Build the container image docker build -t grub

Christopher Gruber 0 Oct 16, 2021
many tools implemented in Golang

Go-Tools many tools implemented in Golang tools project comments abtest AB测试分流 dag_flow DAG工作流 hugo_themes Hugo主题 log_monitor 日志监控服务 pepper_cache 内存kv

殷雅俊 0 Dec 12, 2021
A Twirp RPC OpenAPI generator implemented as `protoc` plugin

twirp-openapi-gen A Twirp RPC OpenAPI generator implemented as protoc plugin Currently supports only OpenAPI 2.0 Usage Installing the generator for pr

Albenik's Golang Projects 1 May 26, 2022
A simple FTP protocol with client and server implemented in TypeScript and Golang

websocket-ftp A simple FTP protocol with client and server implemented in TypeScript and Golang. Example (Client) const buffer: Uint8Array = (new Text

LQR471814 0 Apr 14, 2022
Pacemaker - Rate limit library. Currently implemented rate limits are

PaceMaker Rate limit library. Currently implemented rate limits are Fixed window

Marquitos 2 Apr 2, 2022