x

package
v1.0.0-...-e80de26 Latest Latest
Warning

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

Go to latest
Published: Sep 30, 2015 License: MIT Imports: 9 Imported by: 5

Documentation

Overview

Package x stores utility functions, mostly for internal usage.

Index

Examples

Constants

View Source
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 Log

func Log(p string) *logrus.Entry

Log returns a logrus.Entry with a package field set.

func LogErr

func LogErr(entry *logrus.Entry, err error) *logrus.Entry

LogErr returns a logrus.Entry with an error field set.

func ParseIdFromUrl

func ParseIdFromUrl(r *http.Request, urlToken string) (uid string, ok bool)

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"

	"gopkg.in/manishrjain/gocrud.v1/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

func UniqueString(alpha int) string

UniqueString generates a unique string only using the characters from alphachars constant, with length as specified.

Example
package main

import (
	"fmt"

	"gopkg.in/manishrjain/gocrud.v1/x"
)

func main() {
	u := x.UniqueString(3)
	fmt.Println(len(u))
}
Output:

3

Types

type Doc

type Doc struct {
	Kind   string
	Id     string
	NanoTs int64
	Data   interface{}
}

Doc is the format data gets stored in search engine.

type Entity

type Entity struct {
	Kind string
	Id   string
}

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"

	"gopkg.in/manishrjain/gocrud.v1/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"

	"gopkg.in/manishrjain/gocrud.v1/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

func (Its) Len

func (its Its) Len() int

func (Its) Less

func (its Its) Less(i, j int) bool

func (Its) Swap

func (its Its) Swap(i, j int)

type Status

type Status struct {
	Code    string `json:"code"`
	Message string `json:"message"`
}

Status stores any error codes retuned along with the error message; and is converted to JSON and returned if there's any error during the result.WriteJsonResponse call.

Jump to

Keyboard shortcuts

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