...

Source file src/github.com/leodido/go-urn/examples_test.go

Documentation: github.com/leodido/go-urn

     1  package urn_test
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/leodido/go-urn"
     7  )
     8  
     9  func ExampleParse() {
    10  	var uid = "URN:foo:a123,456"
    11  
    12  	if u, ok := urn.Parse([]byte(uid)); ok {
    13  		fmt.Println(u.ID)
    14  		fmt.Println(u.SS)
    15  		fmt.Println(u.SCIM())
    16  	}
    17  
    18  	// Output: foo
    19  	// a123,456
    20  	// <nil>
    21  }
    22  
    23  func ExampleURN_MarshalJSON() {
    24  	var uid = "URN:foo:a123,456"
    25  
    26  	if u, ok := urn.Parse([]byte(uid)); ok {
    27  		json, err := u.MarshalJSON()
    28  		if err != nil {
    29  			panic("invalid urn")
    30  		}
    31  		fmt.Println(string(json))
    32  	}
    33  
    34  	// Output: "URN:foo:a123,456"
    35  }
    36  
    37  func ExampleURN_Equal() {
    38  	var uid1 = "URN:foo:a123,456"
    39  	var uid2 = "URN:FOO:a123,456"
    40  
    41  	u1, ok := urn.Parse([]byte(uid1))
    42  	if !ok {
    43  		panic("invalid urn")
    44  	}
    45  
    46  	u2, ok := urn.Parse([]byte(uid2))
    47  	if !ok {
    48  		panic("invalid urn")
    49  	}
    50  
    51  	if u1.Equal(u2) {
    52  		fmt.Printf("%s equals %s", u1.String(), u2.String())
    53  	}
    54  
    55  	// Output: URN:foo:a123,456 equals URN:FOO:a123,456
    56  }
    57  
    58  func ExampleParse_scim() {
    59  	input := "urn:ietf:params:scim:api:messages:2.0:ListResponse"
    60  
    61  	u, ok := urn.Parse([]byte(input), urn.WithParsingMode(urn.RFC7643Only))
    62  	if !ok {
    63  		panic("invalid SCIM urn")
    64  	}
    65  	data, err := u.MarshalJSON()
    66  	if err != nil {
    67  		panic("couldn't marshal")
    68  	}
    69  	fmt.Println(string(data))
    70  	fmt.Println(u.IsSCIM())
    71  	scim := u.SCIM()
    72  	fmt.Println(scim.Type.String())
    73  	fmt.Println(scim.Name)
    74  	fmt.Println(scim.Other)
    75  
    76  	// Output:
    77  	// "urn:ietf:params:scim:api:messages:2.0:ListResponse"
    78  	// true
    79  	// api
    80  	// messages
    81  	// 2.0:ListResponse
    82  }
    83  

View as plain text