...

Text file src/github.com/leodido/go-urn/README.md

Documentation: github.com/leodido/go-urn

     1[![Build](https://img.shields.io/circleci/build/github/leodido/go-urn?style=for-the-badge)](https://app.circleci.com/pipelines/github/leodido/go-urn) [![Coverage](https://img.shields.io/codecov/c/github/leodido/go-urn.svg?style=for-the-badge)](https://codecov.io/gh/leodido/go-urn) [![Documentation](https://img.shields.io/badge/godoc-reference-blue.svg?style=for-the-badge)](https://godoc.org/github.com/leodido/go-urn)
     2
     3**A parser for URNs**.
     4
     5> As seen on [RFC 2141](https://datatracker.ietf.org/doc/html/rfc2141), [RFC 7643](https://datatracker.ietf.org/doc/html/rfc7643#section-10), and on [RFC 8141](https://datatracker.ietf.org/doc/html/rfc8141).
     6
     7[API documentation](https://godoc.org/github.com/leodido/go-urn).
     8
     9Starting with version 1.3 this library also supports [RFC 7643 SCIM URNs](https://datatracker.ietf.org/doc/html/rfc7643#section-10).
    10
    11Starting with version 1.4 this library also supports [RFC 8141 URNs (2017)](https://datatracker.ietf.org/doc/html/rfc8141).
    12
    13## Installation
    14
    15```
    16go get github.com/leodido/go-urn
    17```
    18
    19## Features
    20
    211. RFC 2141 URNs parsing (default)
    222. RFC 8141 URNs parsing (supersedes RFC 2141)
    233. RFC 7643 SCIM URNs parsing
    244. Normalization as per RFCs
    255. Lexical equivalence as per RFCs
    266. Precise, fine-grained errors
    27
    28## Performances
    29
    30This implementation results to be really fast.
    31
    32Usually below 400 ns on my machine<sup>[1](#mymachine)</sup>.
    33
    34Notice it also performs, while parsing:
    35
    361. fine-grained and informative erroring
    372. specific-string normalization
    38
    39```
    40ok/00/urn:a:b______________________________________/-10    51372006    109.0 ns/op    275 B/op    3 allocs/op
    41ok/01/URN:foo:a123,456_____________________________/-10    36024072    160.8 ns/op    296 B/op    6 allocs/op
    42ok/02/urn:foo:a123%2C456___________________________/-10    31901007    188.4 ns/op    320 B/op    7 allocs/op
    43ok/03/urn:ietf:params:scim:schemas:core:2.0:User___/-10    22736756    266.6 ns/op    376 B/op    6 allocs/op
    44ok/04/urn:ietf:params:scim:schemas:extension:enterp/-10    18291859    335.2 ns/op    408 B/op    6 allocs/op
    45ok/05/urn:ietf:params:scim:schemas:extension:enterp/-10    15283087    379.4 ns/op    440 B/op    6 allocs/op
    46ok/06/urn:burnout:nss______________________________/-10    39407593    155.1 ns/op    288 B/op    6 allocs/op
    47ok/07/urn:abcdefghilmnopqrstuvzabcdefghilm:x_______/-10    27832718    211.4 ns/op    307 B/op    4 allocs/op
    48ok/08/urn:urnurnurn:urn____________________________/-10    33269596    168.1 ns/op    293 B/op    6 allocs/op
    49ok/09/urn:ciao:!!*_________________________________/-10    41100675    148.8 ns/op    288 B/op    6 allocs/op
    50ok/10/urn:ciao:=@__________________________________/-10    37214253    149.7 ns/op    284 B/op    6 allocs/op
    51ok/11/urn:ciao:@!=%2C(xyz)+a,b.*@g=$_'_____________/-10    26534240    229.8 ns/op    336 B/op    7 allocs/op
    52ok/12/URN:x:abc%1Dz%2F%3az_________________________/-10    28166396    211.8 ns/op    336 B/op    7 allocs/op
    53no/13/URN:---xxx:x_________________________________/-10    23635159    255.6 ns/op    419 B/op    5 allocs/op
    54no/14/urn::colon:nss_______________________________/-10    23594779    258.4 ns/op    419 B/op    5 allocs/op
    55no/15/URN:@,:x_____________________________________/-10    23742535    261.5 ns/op    419 B/op    5 allocs/op
    56no/16/URN:URN:NSS__________________________________/-10    27432714    223.3 ns/op    371 B/op    5 allocs/op
    57no/17/urn:UrN:NSS__________________________________/-10    26922117    224.9 ns/op    371 B/op    5 allocs/op
    58no/18/urn:a:%______________________________________/-10    24926733    224.6 ns/op    371 B/op    5 allocs/op
    59no/19/urn:urn:NSS__________________________________/-10    27652641    220.7 ns/op    371 B/op    5 allocs/op
    60```
    61
    62* <a name="mymachine">[1]</a>: Apple M1 Pro
    63
    64
    65## Example
    66
    67For more examples take a look at the [examples file](examples_test.go).
    68
    69
    70```go
    71package main
    72
    73import (
    74	"fmt"
    75	"github.com/leodido/go-urn"
    76)
    77
    78func main() {
    79	var uid = "URN:foo:a123,456"
    80
    81    // Parse the input string as a RFC 2141 URN only
    82	u, e := urn.NewMachine().Parse(uid)
    83	if e != nil {
    84		fmt.Errorf(err)
    85
    86		return
    87	}
    88
    89	fmt.Println(u.ID)
    90	fmt.Println(u.SS)
    91
    92	// Output:
    93	// foo
    94	// a123,456
    95}
    96```
    97
    98```go
    99package main
   100
   101import (
   102	"fmt"
   103	"github.com/leodido/go-urn"
   104)
   105
   106func main() {
   107	var uid = "URN:foo:a123,456"
   108
   109    // Parse the input string as a RFC 2141 URN only
   110	u, ok := urn.Parse([]byte(uid))
   111	if !ok {
   112		panic("error parsing urn")
   113	}
   114
   115	fmt.Println(u.ID)
   116	fmt.Println(u.SS)
   117
   118	// Output:
   119	// foo
   120	// a123,456
   121}
   122```
   123
   124```go
   125package main
   126
   127import (
   128	"fmt"
   129	"github.com/leodido/go-urn"
   130)
   131
   132func main() {
   133	input := "urn:ietf:params:scim:api:messages:2.0:ListResponse"
   134
   135	// Parsing the input string as a RFC 7643 SCIM URN
   136	u, ok := urn.Parse([]byte(input), urn.WithParsingMode(urn.RFC7643Only))
   137	if !ok {
   138		panic("error parsing urn")
   139	}
   140
   141	fmt.Println(u.IsSCIM())
   142	scim := u.SCIM()
   143	fmt.Println(scim.Type.String())
   144	fmt.Println(scim.Name)
   145	fmt.Println(scim.Other)
   146
   147	// Output:
   148	// true
   149	// api
   150	// messages
   151	// 2.0:ListResponse
   152}
   153```

View as plain text