nsid

package module
v0.0.0-...-bbb9075 Latest Latest
Warning

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

Go to latest
Published: May 6, 2025 License: MIT Imports: 3 Imported by: 2

README

go-nsid

Package nsid provides an implementation of BlueSky's AT-Protocol's NSID (Namespaced Identifier), for the Go programming language.

NSIDs are used by the Bluesky network and its AT-protocol, and are defined at: https://atproto.com/specs/nsid

Documention

Online documentation, which includes examples, can be found at: http://godoc.org/github.com/reiver/go-nsid

GoDoc

NSID

In plain language, an NSID (Namespaced Identifier) is a combination of Internet domain-name (such as example.com) and a name (such as fooBar).

The NSID (Namespaced Identifier) calls the Internet domain-name part the domain-authority. Although, in an NSID the Internet domain-name (such as example.com) is written in reverse-order (such as com.example) for the domain-authority.

NSID Example 1

For example, if the Internet domain-name was:

example.com

And the name was:

fooBar

Then the resulting NSID (Namespaced Identifier) would be:

com.example.fooBar

Note that the Internet domain-name (example.com) was written in reverse-order (com.example) before constructing the NSID (Namespaced Identifier).

NSID Example 2

Here is another example. If the Internet domain-name was:

video.archive.org

Note that we are using a sub-domain of example.com here.

And if the name was:

clipVideo

Then the resulting NSID (Namespaced Identifier) would be:

org.archive.video.clipVideo

Again note that the Internet domain-name (video.archive.org) was written in reverse-order (org.archive.video) before constructing the NSID (Namespaced Identifier).

Examples

To split an NSID into its domain-authority and name:

import "github.com/reiver/go-nsid"

// ...

domainAuthority, name := nsid.Split("com.example.fooBar")

// domainAuthority == "com.example"
// name == "fooBar"

To normalize an NSID:

import "github.com/reiver/go-nsid"

// ...

value := nsid.Normalize("COM.Example.fooBar")

// value == "com.example.fooBar"

To validate an NSID:

import "github.com/reiver/go-nsid"

// ...

err := nsid.Validate("com.example.fooBar")
if nil != err {
	return err
}

// value == "com.example.fooBar"

Import

To import package nsid use import code like the follownig:

import "github.com/reiver/go-nsid"

Installation

To install package nsid do the following:

GOPROXY=direct go get https://github.com/reiver/go-nsid

Author

Package nsid was written by Charles Iliya Krempeaux

See Also

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Construct

func Construct(values ...string) string

Construct creates an NSID (Namespaced Identifier).

The NSID (Namespaced Identifier) is part of BlueSky's AT-Protocol, as defined here: https://atproto.com/specs/nsid

Construct is similar to Join, but is more flexible.

Construct allows the domain-authority to be broken into parts.

Note that the NSID that Construct returns is NOT normalized. Use ConstructAndNormalize to create a normalized NSID.

If you are not sure whether to use Construct or ConstructAndNormalize, use ConstructAndNormalize.

Example
package main

import (
	"fmt"

	"github.com/reiver/go-nsid"
)

func main() {

	nsID := nsid.Construct("org.archive", "video.streaming", "createStream")

	fmt.Printf("nsid: %s\n", nsID)

}
Output:
nsid: org.archive.video.streaming.createStream

func ConstructAndNormalize

func ConstructAndNormalize(values ...string) string

ConstructAndNormalize and similar to Construct but it also normalizes the resulting NSID (Namespaced Identifier).

ConstructAndNormalize creates an NSID (Namespaced Identifier).

The NSID (Namespaced Identifier) is part of BlueSky's AT-Protocol, as defined here: https://atproto.com/specs/nsid

ConstructAndNormalize is similar to Join, but is more flexible.

ConstructAndNormalize allows the domain-authority to be broken into parts.

If you are not sure whether to use Construct or ConstructAndNormalize, use ConstructAndNormalize.

Example
package main

import (
	"fmt"

	"github.com/reiver/go-nsid"
)

func main() {

	nsID := nsid.ConstructAndNormalize("org.archive", "video.streaming", "createStream")

	fmt.Printf("nsid: %s\n", nsID)

}
Output:
nsid: org.archive.video.streaming.createStream

func Join

func Join(domainAuthority string, name string) string

Join combines an NSID domain-authority (such as "com.example") with an NSID name (such as "fooBar") to construct an NSID (such as "com.example.fooBar").

The NSID (Namespaced Identifier) is part of BlueSky's AT-Protocol, as defined here: https://atproto.com/specs/nsid

Join more-or-less does the opposite of Split.

Note that the NSID that Join returns is NOT normalized. Use JoinAndNormalize to create a normalized NSID.

Join does not validate the overall resulting NSID. To validate the overall resulting NSID call Validate.

If you are not sure whether to use Join or JoinAndNormalize, use JoinAndNormalize.

Example
package main

import (
	"fmt"

	"github.com/reiver/go-nsid"
)

func main() {

	var domainAuthority string = "com.example"
	var name string = "fooBar"

	nsID := nsid.Join(domainAuthority, name)

	fmt.Printf("domain-authority: %s\n", domainAuthority)
	fmt.Printf("name:                         %s\n", name)
	fmt.Printf("nsid:             %s\n", nsID)

}
Output:
domain-authority: com.example
name:                         fooBar
nsid:             com.example.fooBar

func JoinAndNormalize

func JoinAndNormalize(domainAuthority string, name string) string

JoinAndNormalize and similar to Join but it also normalizes the resulting NSID (Namespaced Identifier).

The NSID (Namespaced Identifier) is part of BlueSky's AT-Protocol, as defined here: https://atproto.com/specs/nsid

JoinAndNormalize combines an NSID domain-authority (such as "com.example") with an NSID name (such as "fooBar") to construct an NSID (such as "com.example.fooBar").

Join more-or-less does the opposite of Split.

Note that the NSID that Join returns is normalized.

Join does not validate the overall resulting NSID. To validate the overall resulting NSID call Validate.

If you are not sure whether to use Join or JoinAndNormalize, use JoinAndNormalize.

Example
package main

import (
	"fmt"

	"github.com/reiver/go-nsid"
)

func main() {

	var domainAuthority string = "com.example"
	var name string = "fooBar"

	nsID := nsid.JoinAndNormalize(domainAuthority, name)

	fmt.Printf("domain-authority: %s\n", domainAuthority)
	fmt.Printf("name:                         %s\n", name)
	fmt.Printf("nsid:             %s\n", nsID)

}
Output:
domain-authority: com.example
name:                         fooBar
nsid:             com.example.fooBar

func Normalize

func Normalize(value string) string

Normalize returns the normalized form of an NSID, as defined in: https://atproto.com/specs/nsid

Normalize does NOT validate the NSID. To validate, call Validate.

An example of a non-normalized NSID domain-authority would be "COM.Example.fooBar". Normalizing that non-normalized NSID domain-authority would result in "com.example.fooBar".

Example
package main

import (
	"fmt"

	"github.com/reiver/go-nsid"
)

func main() {

	var nsID string = "COM.Example.fooBar"

	normalized := nsid.Normalize(nsID)

	fmt.Printf("original nsid:   %s\n", nsID)
	fmt.Printf("noramlized nsid: %s\n", normalized)

}
Output:
original nsid:   COM.Example.fooBar
noramlized nsid: com.example.fooBar

func NormalizeDomainAuthority

func NormalizeDomainAuthority(value string) string

NormalizeDomainAuthority returns the normalized form of an domain-authority, as defined in: https://atproto.com/specs/nsid

The domain-authority (such as "com.example") is part of an NSID (such as "com.example.fooBar").

In simple language, you can think of an NSID domain-authority as an Internet domain-name written in reverse-order. For example, if the Internet domain-name was "example.com", then the NSID domain-authority would be "com.example".

An example of a non-normalized NSID domain-authority would be "COM.Example". Normalizing that non-normalized NSID domain-authority would result in "com.example".

Note that if you want to normalize a whole NSID rather than just a domain-authority, then instead use Normalize.

Example
package main

import (
	"fmt"

	"github.com/reiver/go-nsid"
)

func main() {

	var domainAuthority string = "COM.Example"

	normalized := nsid.NormalizeDomainAuthority(domainAuthority)

	fmt.Printf("original domain-authority:   %s\n", domainAuthority)
	fmt.Printf("noramlized domain-authority: %s\n", normalized)

}
Output:
original domain-authority:   COM.Example
noramlized domain-authority: com.example

func Split

func Split(value string) (domainAuthority string, name string)

Split returns the domain-authority and name of an NSID, as defined here: https://atproto.com/specs/nsid

Split does NOT normalizes the domain-authority of the NSID that it returns. Use SplitAndNormalize to have the domain-authority normalized.

If you are not sure whether to use Split or [SpliAndNormalize], use SplitAndNormalize.

Example
package main

import (
	"fmt"

	"github.com/reiver/go-nsid"
)

func main() {

	var nsID string = "COM.Example.fooBar"

	domainAuthority, name := nsid.Split(nsID)

	fmt.Printf("nsid:             %s\n", nsID)
	fmt.Printf("domain-authority: %s\n", domainAuthority)
	fmt.Printf("name:                         %s\n", name)

}
Output:
nsid:             COM.Example.fooBar
domain-authority: COM.Example
name:                         fooBar

func SplitAndNormalize

func SplitAndNormalize(value string) (domainAuthority string, name string)

Split returns the domain-authority and name of an NSID, as defined here: https://atproto.com/specs/nsid

Split normalizes the domain-authority of the NSID that it returns.

If you are not sure whether to use Split or SpliAndNormalize, use SplitAndNormalize.

Example
package main

import (
	"fmt"

	"github.com/reiver/go-nsid"
)

func main() {

	var nsID string = "COM.Example.fooBar"

	domainAuthority, name := nsid.SplitAndNormalize(nsID)

	fmt.Printf("nsid:             %s\n", nsID)
	fmt.Printf("domain-authority: %s\n", domainAuthority)
	fmt.Printf("name:                         %s\n", name)

}
Output:
nsid:             COM.Example.fooBar
domain-authority: com.example
name:                         fooBar

func Validate

func Validate(value string) error

Validate returns an error if the NSID is invalid. It returns nil if the NSID is valid.

The validation rules for an NSID are defined here: https://atproto.com/specs/nsid

Example
package main

import (
	"fmt"

	"github.com/reiver/go-nsid"
)

func main() {

	var nsID string = "com.example.foo-bar"

	err := nsid.Validate(nsID)

	fmt.Printf("nsid: %s\n", nsID)
	fmt.Printf("validation error: %s\n", err)

}
Output:
nsid: com.example.foo-bar
validation error: nsid: character №3 ('-') (U+002D) of name ("foo-bar") of nsid ("com.example.foo-bar") cannot be a hyphen but must instead be an upper-case letter ('A'-'Z'), or lower-case letter ('a'-'z'), or digits ('0'-'9')

Types

type NSID

type NSID string

NSID represents a NSID (Namespaced Identifier).

The NSID (Namespaced Identifier) is part of BlueSky's AT-Protocol, as defined here: https://atproto.com/specs/nsid

func CreateNSID

func CreateNSID(values ...string) (NSID, error)

CreateNSID creates an NSID (Namespaced Identifier).

The NSID (Namespaced Identifier) is part of BlueSky's AT-Protocol, as defined here: https://atproto.com/specs/nsid

func MustCreateNSID

func MustCreateNSID(values ...string) NSID

MustCreateNSID is similar to CreateNSID expect it panic()s if thre is an error.

The NSID (Namespaced Identifier) is part of BlueSky's AT-Protocol, as defined here: https://atproto.com/specs/nsid

func (NSID) DomainAuthority

func (receiver NSID) DomainAuthority() string

DomainAuthority returns the domain-authority of an NSID.

The NSID (Namespaced Identifier) is part of BlueSky's AT-Protocol, as defined here: https://atproto.com/specs/nsid

func (NSID) Name

func (receiver NSID) Name() string

Name returns the domain-authority of an NSID.

The NSID (Namespaced Identifier) is part of BlueSky's AT-Protocol, as defined here: https://atproto.com/specs/nsid

func (NSID) Split

func (receiver NSID) Split() (domainAuthority string, name string)

Split returns the domain-authority and name of an NSID.

The NSID (Namespaced Identifier) is part of BlueSky's AT-Protocol, as defined here: https://atproto.com/specs/nsid

func (NSID) Validate

func (receiver NSID) Validate() error

Validate returns an error if the NSID is invalid. It returns nil if the NSID is valid.

The NSID (Namespaced Identifier) is part of BlueSky's AT-Protocol, as defined here: https://atproto.com/specs/nsid

Jump to

Keyboard shortcuts

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