Documentation
¶
Overview ¶
go-dataurl ¶
Package dataurl provides tools to work with data URLs, as defined by RFC 2397 ( http://www.faqs.org/rfcs/rfc2397.html ).
Here are some data URL examples:
data:,Hello%20world! data:text/html;base64,PGh0bWw+PGhlYWQ+PHRpdGxlPlRlc3Q8L3RpdGxlPjwvaGVhZD48Ym9keT48cD5UaGlzIGlzIGEgdGVzdDwvYm9keT48L2h0bWw+Cg== data:text/plain;charset=utf-8,This%20is%20a%20test%21 data:;charset=utf-8,This%20is%20a%20test%21 data:text/plain,This%20is%20a%20test%21 data:,This%20is%20a%20test%21 data:text/plain;charset=utf-8;base64,VGhpcyBpcyBhIHRlc3Qh data:;charset=utf-8;base64,VGhpcyBpcyBhIHRlc3Qh data:text/plain;base64,VGhpcyBpcyBhIHRlc3Qh data:;base64,VGhpcyBpcyBhIHRlc3Qh
Example Usage
parcel, err := dataurl.Parse("data:,Hello%20world!") if nil != err { //@TODO } fmt.Printf("Content: %q \n", parcel.String()) // parcel.String() == "Hello world!" fmt.Printf("Media Type: %q \n", parcel.MediaType()) // parcel.MediaType() == "text/plain;charset=US-ASCII"
Another Example Usage
// Note that dataurl.MustPasre() will panic() if there is an // error when trying to parse the data URL! parcel := dataurl.MustParse("data:,Hello%20world!") // parcel.String() == "Hello world!" fmt.Printf("Content: %q \n", parcel.String()) fmt.Printf("Media Type: %q \n", parcel.MediaType()) // parcel.MediaType() == "text/plain;charset=US-ASCII"
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type BadMediaTypeComplainer ¶
type BadMediaTypeComplainer interface { BadRequestComplainer BadMediaTypeComplainer() WrappedError() error }
type BadRequestComplainer ¶
type BadRequestComplainer interface { error BadRequestComplainer() }
type InternalErrorComplainer ¶
type InternalErrorComplainer interface { error InternalErrorComplainer() }
type NotADataUrlComplainer ¶
type NotADataUrlComplainer interface { BadRequestComplainer NotADataUrlComplainer() }
type Parcel ¶
type Parcel interface { Bytes() []byte Reader() io.Reader Runes() []rune String() string MediaType() string }
Parcel is used to contain the result of parsing a Data URL.
It provides the Bytes, Reader, Runes and String methods; used to retrieve the contents of a Data URL in []byte, io.Reader, []rune and string formats, respectively.
It also provides the MediaType method; used to retrieve the (explicitly or implicitly) declared 'media type' of a Data URL.
For example:
parcel, err := dataurl.Parse("data:,Hello") if nil != err { //@TODO } contents := parcel.String() // == "Hello" mediaType := parcel.MediaType() // == "text/plain;charset=US-ASCII"
Also, for example:
parcel, err := dataurl.Parse("data:application/x-apple-banana-cherry,Hello") if nil != err { //@TODO } contents := parcel.String() // == "Hello" mediaType := parcel.MediaType() // == "application/x-apple-banana-cherry;charset=US-ASCII"
func MustParse ¶
MustParse is like dataurl.Parse(), expect it only returns a Parcel, and panic()s if there was an error parsing 'dataURL'.
Example usage:
parcel := dataurl.MustParse("data:,Hello%20world!") fmt.Println(parcel.String()) // parcel.String() == "Hello world!"
func Parse ¶
Parse parses a data URL contained in parameter 'dataURL', and if it contained a valid data URL, returns a Parcel, else returns an error.
Example usage:
parcel, err := dataurl.Parse("data:,Hello%20world!") if nil != err { //@TODO } fmt.Println(parcel.String()) // parcel.String() == "Hello world!"
type SyntaxErrorComplainer ¶
type SyntaxErrorComplainer interface { BadRequestComplainer SyntaxErrorComplainer() }
SyntaxErrorComplainer is used to represent a specific kind of BadRequestComplainer error. Specifically, it represents a syntax error in a data URL passed to the dataurl.Parse() func or the dataurl.MustParse() func.
Example usage is as follows:
parcel, err := dataurl.Parse("data:,Hello%20world!") if nil != err { switch err.(type) { case dataurl.SyntaxErrorComplainer: // ← Here we are detecting if the error returned was due to a syntax error, in the data URL. Also note that it comes BEFORE the 'dataurl.BadRequestComplainer' case; THAT IS IMPORTANT! fmt.Printf("The data URL passed to dataurl.Parse() had a syntax error in it. The error message describing the syntax error is....\n%s\n", err.Error()) return case dataurl.BadRequestComplainer: fmt.Printf("Something you did when you called dataurl.Parse() caused an error. The error message was....\n%s\n", err.Error()) return case dataurl.InternalErrorComplainer: fmt.Printf("It's not your fault; it's my fault. Something bad happened internally when dataurl.Parse() was running. The error message was....\n%s\n", err.Error()) return default: fmt.Printf("Some kind of unexpected error happend: %v", err) return } }