Skip to content

Instantly share code, notes, and snippets.

@skriptble
Created March 22, 2015 17:59
Show Gist options
  • Save skriptble/6bb0f97441aa879f94ee to your computer and use it in GitHub Desktop.
Save skriptble/6bb0f97441aa879f94ee to your computer and use it in GitHub Desktop.
Full example of building the alps-contact example alps profile in Go.
package main
import (
"bytes"
"encoding/json"
"net/url"
"os"
"github.com/skriptble/hyper/profiles/alps"
"github.com/skriptble/hyper/profiles/alps/constructor"
)
func main() {
docOpt := constructor.NewDoc(nil, "text", "input for searching")
// Semantic is the default but we'll set it manually as well.
typeOpt := constructor.NewType(alps.Semantic)
nameDescriptor, err := constructor.NewDescriptor("name", nil, typeOpt, docOpt)
if err != nil {
panic(err)
}
// Since we've already added the previous doc to a descriptor we can reuse the
// variable
docOpt = constructor.NewDoc(nil, "text", "search for contacts in the list")
typeOpt = constructor.NewType(alps.Safe)
rtOpt := constructor.NewRt("contacts")
searchDescriptor, err := constructor.NewDescriptor("search", nil, docOpt, typeOpt, rtOpt, nameDescriptor)
if err != nil {
panic(err)
}
typeOpt = constructor.NewType(alps.Safe)
docOpt = constructor.NewDoc(nil, "text", "link to contact")
itemDescriptor, err := constructor.NewDescriptor("item", nil, typeOpt, docOpt)
if err != nil {
panic(err)
}
typeOpt = constructor.NewType(alps.Semantic)
// We use *url.URL to create hrefs, so we'll parse and then pass it in
href, err := url.Parse("http://schema.org/givenName")
if err != nil {
panic(err)
}
givenDescriptor, err := constructor.NewDescriptor("givenName", href, typeOpt)
if err != nil {
panic(err)
}
// Since each href has the same base, we can safely reuse the href and just
// update the path, alternatively one could reparse the whole url.
href.Path = "/familyName"
// We can also reuse the typeOpt as many times as we wish, each Option uses the
// value and encodes it in the various elements making it safe to reuse.
familyDescriptor, err := constructor.NewDescriptor("familyName", href, typeOpt)
if err != nil {
panic(err)
}
href.Path = "/email"
emailDescriptor, err := constructor.NewDescriptor("email", href, typeOpt)
if err != nil {
panic(err)
}
href.Path = "/telephone"
telephoneDescriptor, err := constructor.NewDescriptor("telephone", href, typeOpt)
if err != nil {
panic(err)
}
// Finally we assemble the "contacts" descriptor
// We should have added each descriptor to a slice and passed in the slice using
// a variadic parameter
docOpt = constructor.NewDoc(nil, "text", "contact item")
contactsDescriptor, err := constructor.NewDescriptor("contacts", nil, docOpt, typeOpt, itemDescriptor, givenDescriptor, familyDescriptor, emailDescriptor, telephoneDescriptor)
if err != nil {
panic(err)
}
docOpt = constructor.NewDoc(nil, "text", "List of contacts w/ search")
contactsProfile, err := constructor.NewProfile(docOpt, searchDescriptor, contactsDescriptor)
if err != nil {
panic(err)
}
result, err := json.Marshal(contactsProfile)
if err != nil {
panic(err)
}
var out bytes.Buffer
json.Indent(&out, result, "", "\t")
out.WriteTo(os.Stdout)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment