querytron

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

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

Go to latest
Published: Jan 21, 2019 License: MIT Imports: 5 Imported by: 5

README

Querytron

Travis CI

Following hot on the successes of Envirotron, Querytron is here to save the day!

Ever needed to deal with a remote system that dealt in both application/json AND application/x-www-form-urlencoded data?

No? I see you've never had the misfortune of integrating with OAuth2 providers! Good on you then.

For the rest of us, I wrote Querytron. It works a lot like Envirotron:

package thing

import (
  "fmt"
  qs "github.com/jhunt/go-querytron"
)

type Response struct {
  Error string `qs:"error"`
  URI   string `qs:"error_uri"`
}

func main() {
  url := SomeFunction()
  var r Response
  qs.Override(&r, url.Query())

  fmt.Printf("error %s (see also %s)\n", c.Error, c.URI)
}

Querytron also works in the other direction, generating querystrings for you, from structure definitions. Given a structure like:

type Example struct {
  Query string `qs:"q"`
  Limit *int   `qs:"limit"`
  Fuzzy *bool  `qs:"fuzzy:yes"`
}

A call to qs.Generate(&Example{...}) will generate a url.Values object and return it, such that:

  • q=... is set if Query is anything besides the empty string
  • limit=... is set if Limit is a non-nil int pointer
  • fuzzy=y is set if Fuzzy is non-nil and points to true

This should make query-string based API client interfaces easier to write.

Oh, and if you (like me) are a bit miffed that Go doesn't let you take the address of literals, there's a whole suite of address-taking functions, like qs.Int(...), qs.Uint64(), etc., as well as two pointer-booleans, qs.True and qs.False.

Happy Hacking!

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	True  *bool
	False *bool
)

Functions

func Generate

func Generate(thing interface{}) url.Values

func Int

func Int(v int) *int

func Int16

func Int16(v int16) *int16

func Int32

func Int32(v int32) *int32

func Int64

func Int64(v int64) *int64

func Int8

func Int8(v int8) *int8

func Override

func Override(thing interface{}, q url.Values)

func Uint

func Uint(v uint) *uint

func Uint16

func Uint16(v uint16) *uint16

func Uint32

func Uint32(v uint32) *uint32

func Uint64

func Uint64(v uint64) *uint64

func Uint8

func Uint8(v uint8) *uint8

Types

This section is empty.

Jump to

Keyboard shortcuts

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