fediverseid

package module
v0.0.0-...-694e7ca Latest Latest
Warning

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

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

README

go-fediverseid

Package fediverseid implements tools for working with Fediverse-IDs, for the Go programming language.

Fediverse-IDs look like these:

  • @reiver@mastodon.social
  • @joeblow@example.com
  • @dariush@host.example
  • @malekeh@host.example

Documention

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

GoDoc

Origin

Fediverse-ID are derived from Twitter-IDs, such:

  • @reiver
  • @twitter
  • @golanggo

Twitter-IDs were not invented by the Twitter company. Instead, users of Twitter started using them on Twitter as a way of replying to other people on Twitter. The Twitter company noticed this, and then (the Twitter company) built this into Twitter.

Twitter users started using Twitter-IDs because this convention of putting an at-symbol ("@") in front of someone's handle to reply to them already existing on blogs, and web-based bulletin boards / forums. Although on blogs, and web-based bulletin boards / forums, the @handle convention was often hyper-linked to the post that was being replied to.

Some people try to trace a history for Fediverse-IDs to e-mail addresses — these people are wrong. E-Mail addresses are compared to Fediverse-IDs as a way of helping people understand what Fediverse-IDs are. The origin of the Fediverse-IDs does not go back to e-mail addresses — it instead goes back to Twitter, blogs, and web-based bulletin boards / forums.

Examples

To parse a Fediverse-ID and split it into its name and host you can do something similar to the following:

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

// ...

fediverseID, err := fediverseid.ParseFediverseIDString("@joeblow@host.example")
if nil != err {
	fmt.Printf("ERROR: problem parsing fediverse-id: %s\n", err)
	return
}

name, found := fediverseID.Name()
if !found {
	fmt.Println("ERROR: missing name")
	return
}

host, found := fediverseID.Host()
if !found {
	fmt.Println("ERROR: missing host")
	return
}

And, to generate a Fediverse-ID from a name and a host you can do something similar to the following:

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

// ...

fid := fediverseid.CreateFediverseID("joeblow", "host.example")

var serializedFediverseID string = fediverseID.String()

Import

To import package fediverseid use import code like the following:

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

Installation

To install package fediverseid do the following:

GOPROXY=direct go get github.com/reiver/go-fediverseid

Author

Package fediverseid was written by Charles Iliya Krempeaux

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FediverseID

type FediverseID struct {
	// contains filtered or unexported fields
}

FediverseID represents a Fediverse-ID.

A (serialized) Fediverse-ID looks similar to this:

@joeblow@host.example

To create a FediverseID use CreateFediverseID or EmptyFediverseID.

To create a pointer to a FediverseID use NewFediverseID or new(fediverseid.FediverseID).

To create a FediverseID from a serialized fediverse-id use ParseFediverseIDBytes or ParseFediverseIDString.

To serialize a FediverseID (to a string) use FediverseID.MarshalText or FediverseID.String.

Example (CreateFediverseID)
const name string = "joeblow"
const host string = "host.example"

var fid fediverseid.FediverseID = fediverseid.CreateFediverseID(name, host)

fmt.Printf("fediverse-id: %s", fid)
Output:

fediverse-id: @joeblow@host.example
Example (EmptyFediverseID)
var fid fediverseid.FediverseID = fediverseid.EmptyFediverseID()

fmt.Printf("fediverse-id (golang code): %#v", fid)
Output:

fediverse-id (golang code): fediverseid.EmptyFediverseID()
Example (NewFediverseID)
const name string = "joeblow"
const host string = "host.example"

var fid *fediverseid.FediverseID = fediverseid.NewFediverseID(name, host)

fmt.Printf("fediverse-id: %s", fid)
Output:

fediverse-id: @joeblow@host.example
Example (NewFediverseID_keyword)
const name string = "joeblow"
const host string = "host.example"

var fid *fediverseid.FediverseID = new(fediverseid.FediverseID).ChainSetName(name).ChainSetHost(host)

fmt.Printf("fediverse-id: %s", fid)
Output:

fediverse-id: @joeblow@host.example
Example (ParseFediverseIDBytes)
var bytes []byte = []byte("@joeblow@host.example")

var fid fediverseid.FediverseID
var err error

fid, err = fediverseid.ParseFediverseIDBytes(bytes)
if nil != err {
	fmt.Printf("ERROR: problem parsing (serialized) fediverse-id %q: %s", bytes, err)
	return
}

fmt.Printf("fediverse-id name: %s\n", fid.NameElse(""))
fmt.Printf("fediverse-id host: %s\n", fid.HostElse(""))
Output:

fediverse-id name: joeblow
fediverse-id host: host.example
Example (ParseFediverseIDString)
var str string = "@joeblow@host.example"

var fid fediverseid.FediverseID
var err error

fid, err = fediverseid.ParseFediverseIDString(str)
if nil != err {
	fmt.Printf("ERROR: problem parsing (serialized) fediverse-id %q: %s", str, err)
	return
}

fmt.Printf("fediverse-id name: %s\n", fid.NameElse(""))
fmt.Printf("fediverse-id host: %s\n", fid.HostElse(""))
Output:

fediverse-id name: joeblow
fediverse-id host: host.example

func CreateFediverseID

func CreateFediverseID(name string, host string) FediverseID

CreateFediverseID creates a FediverseID.

For example:

var name string = "joeblow"
var host string = "host.example"

fid := fediverseid.CreateFediverseID(name, host)
Example
const name string = "joeblow"
const host string = "host.example"

fid := fediverseid.CreateFediverseID(name, host)

var fediverseID string = fid.String()

fmt.Printf("fediverse-id: %s", fediverseID)
Output:

fediverse-id: @joeblow@host.example

func EmptyFediverseID

func EmptyFediverseID() FediverseID

EmptyFediverseID returns an empty FediverseID.

For example:

fid := fediverseid.EmptyFediverseID()

func NewFediverseID

func NewFediverseID(name string, host string) *FediverseID

NewFediverseID returns a new *FediverseID with the `name` and `host` specified.

For example:

var name string = "joeblow"
var host string = "host.example"

fid := fediverseid.NewFediverseID(name, host)

func ParseFediverseIDBytes

func ParseFediverseIDBytes(id []byte) (FediverseID, error)

ParseFediverseIDBytes parses a []byte and (if valid) returns a FediverseID. If not valid, returns an error.

For example:

var value []byte = []byte("@joeblow@host.example")

fid, err := fediverseid.ParseFediverseIDBytes(value)

See also: ParseFediverseIDString

func ParseFediverseIDString

func ParseFediverseIDString(id string) (FediverseID, error)

ParseFediverseIDString parses a string and (if valid) returns a FediverseID. If not valid, returns an error.

For example:

var value string = "@joeblow@host.example"

fid, err := fediverseid.ParseFediverseIDString(value)

See also: ParseFediverseIDBytes

func (FediverseID) AcctURI

func (receiver FediverseID) AcctURI() string

AcctURI returns the acct-uri equivalent of the FediverseID. AcctURI returns an empty string ("") if the receiver isn't a valid Fediverse-ID.

Example
const name string = "joeblow"
const host string = "host.example"

fid := fediverseid.CreateFediverseID(name, host)

var fediverseID string = fid.String()
var acctURI string = fid.AcctURI() // <---------

fmt.Printf("fediverse-id: %s\n", fediverseID)
fmt.Printf("acct-uri: %s\n", acctURI)
Output:

fediverse-id: @joeblow@host.example
acct-uri: acct:joeblow@host.example

func (*FediverseID) ChainSetHost

func (receiver *FediverseID) ChainSetHost(value string) *FediverseID

ChainSetHost sets the (raw) 'host' of the FediverseID, and returns the receiver.

This is useful for chaining.

func (*FediverseID) ChainSetName

func (receiver *FediverseID) ChainSetName(value string) *FediverseID

ChainSetName sets the (raw) 'name' of the FediverseID, and returns the receiver.

This is useful for chaining.

func (*FediverseID) FediverseID

func (receiver *FediverseID) FediverseID() FediverseID

FediverseID turns a *FediverseID (i.e., a pointer to a FediverseID) back into a FediverseID.

For example:

var fid fediveseid.FediverseID = fediveseid.EmptyFediverseID().ChainSetHost("example.com").FediverseID()

func (FediverseID) GoString

func (receiver FediverseID) GoString() string

GoString returns Go code (as a string) that could be used to create this FediverseID.

GoString also makes FediverseID fit the fmt.GoStringer interface. (Which is used by fmt.Errorf, fmt.Fprint, fmt.Fprintf, fmt.Fprintln, fmt.Print, fmt.Printf, fmt.Println, and other similar functions, with the "%#v" format.)

func (FediverseID) Host

func (receiver FediverseID) Host() (string, bool)

Host returns the (raw) 'host' of a Fediverse-ID.

func (FediverseID) HostElse

func (receiver FediverseID) HostElse(alt string) string

HostElse returns the (raw) 'host' of a Fediverse-ID if defined, else returns 'alt'.

func (FediverseID) MarshalText

func (receiver FediverseID) MarshalText() ([]byte, error)

MarshalText returns the (serialized) Fediverse-ID, if valid. Else returns an error.

MarshalText is similar to FediverseID.Serialize except that is returns a []byte rather than a string.

MarshalText is also similar to FediverseID.String except that it returns a []byte and an error if it is invalid.

MarshalText also makes FediverseID fit the encoding.TextMarshaler interface. And thus, among other things, is an alternative to [json.Marshaler].

func (FediverseID) Name

func (receiver FediverseID) Name() (string, bool)

Name returns the (raw) 'name' of a Fediverse-ID.

func (FediverseID) NameElse

func (receiver FediverseID) NameElse(alt string) string

NameElse returns the (raw) 'name' of a Fediverse-ID if defined, else returns 'alt'.

func (FediverseID) Serialize

func (receiver FediverseID) Serialize() (string, error)

Serialize returns the (serialized) Fediverse-ID, if valid. Else returns an error.

Serialize is similar to FediverseID.String except that it returns an error if it is invalid.

Serialize is also similar to FediverseID.MarshalText except that is returns a string rather than a []byte.

func (*FediverseID) SetHost

func (receiver *FediverseID) SetHost(value string)

SetHost sets the (raw) 'host' of the FediverseID.

func (*FediverseID) SetName

func (receiver *FediverseID) SetName(value string)

SetName sets the (raw) 'name' of the FediverseID.

func (FediverseID) String

func (receiver FediverseID) String() string

String returns the (serialized) Fediverse-ID, if valid. Else returns an empty string.

String also makes FediverseID fit the fmt.Stringer interface. (Which is used by fmt.Errorf, fmt.Fprint, fmt.Fprintf, fmt.Fprintln, fmt.Print, fmt.Printf, fmt.Println, and other similar functions.)

See also: FediverseID.Serialize.

Example
const name string = "joeblow"
const host string = "host.example"

fid := fediverseid.CreateFediverseID(name, host)

var fediverseID string = fid.String() // <---------

fmt.Printf("fediverse-id: %s", fediverseID)
Output:

fediverse-id: @joeblow@host.example

func (*FediverseID) UnmarshalText

func (receiver *FediverseID) UnmarshalText(text []byte) error

UnmarshalText unserializes Fediverse-ID, if valid, and sets the value of the receiver to it. Else returns an error.

UnmarshalText is similar to [Unserialize] except that it takes a []byte parameter rather than a string parameter.

UnmarshalText is similar to ParseFediverseIDBytes, except UnmarshalText is a method of FediverseID, where ParseFediverseIDBytes is a standalone function. And because UnmarshalText is similar to ParseFediverseIDBytes, it is also similar to ParseFediverseIDString.

UnmarshalText also makes FediverseID fit the encoding.TextUnmarshaler interface. And thus, among other things, is an alternative to [json.Unmarshaler].

func (*FediverseID) Unserialize

func (receiver *FediverseID) Unserialize(text string) error

Unserialize unserializes Fediverse-ID, if valid, and sets the value of the receiver to it. Else returns an error.

Unserialize is similar to [UnmarshalText] except that it takes a string parameter rather than a []byte parameter.

Unserialize is similar to ParseFediverseIDString, except Unserialize is a method of FediverseID, where ParseFediverseIDString is a standalone function. And because Unserialize is similar to ParseFediverseIDString, it is also similar to ParseFediverseIDBytes.

Jump to

Keyboard shortcuts

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