Documentation
¶
Overview ¶
Package x stores utility functions, mostly for internal usage.
Index ¶
- Constants
- func Log(p string) *logrus.Entry
- func LogErr(entry *logrus.Entry, err error) *logrus.Entry
- func ParseIdFromUrl(r *http.Request, urlToken string) (uid string, ok bool)
- func ParseRequest(w http.ResponseWriter, r *http.Request, data interface{}) bool
- func Reply(w http.ResponseWriter, rep interface{})
- func SetStatus(w http.ResponseWriter, code, msg string)
- func UniqueString(alpha int) string
- type Doc
- type Entity
- type Instruction
- type Its
- type Status
Examples ¶
Constants ¶
const ( E_ERROR = "E_ERROR" E_INVALID_METHOD = "E_INVALID_METHOD" E_INVALID_REQUEST = "E_INVALID_REQUEST" E_INVALID_USER = "E_INVALID_USER" E_MISSING_REQUIRED = "E_MISSING_REQUIRED" E_OK = "E_OK" E_UNAUTHORIZED = "E_UNAUTHORIZED" )
Error constants.
Variables ¶
This section is empty.
Functions ¶
func ParseIdFromUrl ¶
ParseIdFromUrl parses id from url (if it's a suffix) in this format:
url = host/xyz/id, urlToken = /xyz/ => uid = id url = host/a/b/id, urlToken = /b/ => uid = id
Example ¶
package main
import (
"fmt"
"net/http"
"github.com/aslanides/gocrud/x"
)
func main() {
r, err := http.NewRequest("GET", "https://localhost/users/uid_12345", nil)
if err != nil {
panic(err)
}
uid, ok := x.ParseIdFromUrl(r, "/users/")
if !ok {
panic("Unable to parse uid")
}
fmt.Println(uid)
r, err = http.NewRequest("GET", "https://localhost/users/uid_12345/", nil)
if err != nil {
panic(err)
}
uid, ok = x.ParseIdFromUrl(r, "/users/")
if !ok {
panic("Unable to parse uid")
}
fmt.Println(uid)
}
Output: uid_12345 uid_12345/
func ParseRequest ¶
func ParseRequest(w http.ResponseWriter, r *http.Request, data interface{}) bool
ParseRequest parses a JSON based POST or PUT request into the provided Golang interface.
func Reply ¶
func Reply(w http.ResponseWriter, rep interface{})
Reply would JSON marshal the provided rep Go interface object, and write that to http.ResponseWriter. In case of error, call SetStatus with the error.
func SetStatus ¶
func SetStatus(w http.ResponseWriter, code, msg string)
SetStatus creates, converts to JSON, and writes a Status object to http.ResponseWriter.
func UniqueString ¶
UniqueString generates a unique string only using the characters from alphachars constant, with length as specified.
Example ¶
package main
import (
"fmt"
"github.com/aslanides/gocrud/x"
)
func main() {
u := x.UniqueString(3)
fmt.Println(len(u))
}
Output: 3
Types ¶
type Instruction ¶
type Instruction struct {
SubjectId string `json:"subject_id,omitempty"`
SubjectType string `json:"subject_type,omitempty"`
Predicate string `json:"predicate,omitempty"`
Object []byte `json:"object,omitempty"`
ObjectId string `json:"object_id,omitempty"`
NanoTs int64 `json:"nano_ts,omitempty"`
Source string `json:"source,omitempty"`
}
Instruction is the format data gets stored in the underlying data stores.
Example ¶
package main
import (
"fmt"
"github.com/aslanides/gocrud/x"
)
func main() {
var i x.Instruction
i.SubjectId = "sid"
i.SubjectType = "stype"
b, err := i.GobEncode()
if err != nil {
panic(err)
}
var o x.Instruction
if err := o.GobDecode(b); err != nil {
panic(err)
}
fmt.Println(o.SubjectId)
fmt.Println(o.SubjectType)
}
Output: sid stype
func (*Instruction) GobDecode ¶
func (i *Instruction) GobDecode(buf []byte) error
GobDecode decodes Instruction from a byte array.
func (*Instruction) GobEncode ¶
func (i *Instruction) GobEncode() ([]byte, error)
GobEncode converts Instruction to a byte array.
type Its ¶
type Its []Instruction
Its is used for providing a sort interface to []Instruction.
Example ¶
package main
import (
"fmt"
"sort"
"github.com/aslanides/gocrud/x"
)
func main() {
var its []x.Instruction
for t := 0; t < 10; t++ {
var i x.Instruction
i.NanoTs = int64(100 - t)
its = append(its, i)
}
sort.Sort(x.Its(its))
fmt.Println(its[0].NanoTs)
}
Output: 91