Documentation ¶
Overview ¶
Package swag contains a bunch of helper functions for go-openapi and go-swagger projects.
You may also use it standalone for your projects.
- convert between value and pointers for builtin types
- convert from string to builtin types (wraps strconv)
- fast json concatenation
- search in path
- load from file or http
- name mangling
This repo has only few dependencies outside of the standard library:
- YAML utilities depend on gopkg.in/yaml.v2
Index ¶
- Constants
- Variables
- func AddInitialisms(words ...string)
- func Bool(v bool) *bool
- func BoolMap(src map[string]bool) map[string]*bool
- func BoolSlice(src []bool) []*bool
- func BoolValue(v *bool) bool
- func BoolValueMap(src map[string]*bool) map[string]bool
- func BoolValueSlice(src []*bool) []bool
- func BytesToYAMLDoc(data []byte) (interface{}, error)
- func Camelize(word string) (camelized string)
- func ConcatJSON(blobs ...[]byte) []byte
- func ContainsStrings(coll []string, item string) bool
- func ContainsStringsCI(coll []string, item string) bool
- func ConvertBool(str string) (bool, error)
- func ConvertFloat32(str string) (float32, error)
- func ConvertFloat64(str string) (float64, error)
- func ConvertInt16(str string) (int16, error)
- func ConvertInt32(str string) (int32, error)
- func ConvertInt64(str string) (int64, error)
- func ConvertInt8(str string) (int8, error)
- func ConvertUint16(str string) (uint16, error)
- func ConvertUint32(str string) (uint32, error)
- func ConvertUint64(str string) (uint64, error)
- func ConvertUint8(str string) (uint8, error)
- func DynamicJSONToStruct(data interface{}, target interface{}) error
- func FindInGoSearchPath(pkg string) string
- func FindInSearchPath(searchPath, pkg string) string
- func Float64(v float64) *float64
- func Float64Map(src map[string]float64) map[string]*float64
- func Float64Slice(src []float64) []*float64
- func Float64Value(v *float64) float64
- func Float64ValueMap(src map[string]*float64) map[string]float64
- func Float64ValueSlice(src []*float64) []float64
- func FormatBool(value bool) string
- func FormatFloat32(value float32) string
- func FormatFloat64(value float64) string
- func FormatInt16(value int16) string
- func FormatInt32(value int32) string
- func FormatInt64(value int64) string
- func FormatInt8(value int8) string
- func FormatUint16(value uint16) string
- func FormatUint32(value uint32) string
- func FormatUint64(value uint64) string
- func FormatUint8(value uint8) string
- func FromDynamicJSON(data, target interface{}) error
- func FullGoSearchPath() string
- func Int(v int) *int
- func Int32(v int32) *int32
- func Int32Map(src map[string]int32) map[string]*int32
- func Int32Slice(src []int32) []*int32
- func Int32Value(v *int32) int32
- func Int32ValueMap(src map[string]*int32) map[string]int32
- func Int32ValueSlice(src []*int32) []int32
- func Int64(v int64) *int64
- func Int64Map(src map[string]int64) map[string]*int64
- func Int64Slice(src []int64) []*int64
- func Int64Value(v *int64) int64
- func Int64ValueMap(src map[string]*int64) map[string]int64
- func Int64ValueSlice(src []*int64) []int64
- func IntMap(src map[string]int) map[string]*int
- func IntSlice(src []int) []*int
- func IntValue(v *int) int
- func IntValueMap(src map[string]*int) map[string]int
- func IntValueSlice(src []*int) []int
- func IsFloat64AJSONInteger(f float64) bool
- func IsZero(data interface{}) bool
- func JoinByFormat(data []string, format string) []string
- func LoadFromFileOrHTTP(path string) ([]byte, error)
- func LoadFromFileOrHTTPWithTimeout(path string, timeout time.Duration) ([]byte, error)
- func LoadStrategy(path string, local, remote func(string) ([]byte, error)) func(string) ([]byte, error)
- func ReadJSON(data []byte, value interface{}) error
- func SplitByFormat(data, format string) []string
- func SplitHostPort(addr string) (host string, port int, err error)
- func String(v string) *string
- func StringMap(src map[string]string) map[string]*string
- func StringSlice(src []string) []*string
- func StringValue(v *string) string
- func StringValueMap(src map[string]*string) map[string]string
- func StringValueSlice(src []*string) []string
- func Time(v time.Time) *time.Time
- func TimeMap(src map[string]time.Time) map[string]*time.Time
- func TimeSlice(src []time.Time) []*time.Time
- func TimeValue(v *time.Time) time.Time
- func TimeValueMap(src map[string]*time.Time) map[string]time.Time
- func TimeValueSlice(src []*time.Time) []time.Time
- func ToCommandName(name string) string
- func ToDynamicJSON(data interface{}) interface{}
- func ToFileName(name string) string
- func ToGoName(name string) string
- func ToHumanNameLower(name string) string
- func ToHumanNameTitle(name string) string
- func ToJSONName(name string) string
- func ToVarName(name string) string
- func Uint(v uint) *uint
- func Uint32(v uint32) *uint32
- func Uint32Map(src map[string]uint32) map[string]*uint32
- func Uint32Slice(src []uint32) []*uint32
- func Uint32Value(v *uint32) uint32
- func Uint32ValueMap(src map[string]*uint32) map[string]uint32
- func Uint32ValueSlice(src []*uint32) []uint32
- func Uint64(v uint64) *uint64
- func Uint64Map(src map[string]uint64) map[string]*uint64
- func Uint64Slice(src []uint64) []*uint64
- func Uint64Value(v *uint64) uint64
- func Uint64ValueMap(src map[string]*uint64) map[string]uint64
- func Uint64ValueSlice(src []*uint64) []uint64
- func UintMap(src map[string]uint) map[string]*uint
- func UintSlice(src []uint) []*uint
- func UintValue(v *uint) uint
- func UintValueMap(src map[string]*uint) map[string]uint
- func UintValueSlice(src []*uint) []uint
- func WriteJSON(data interface{}) ([]byte, error)
- func YAMLData(path string) (interface{}, error)
- func YAMLDoc(path string) (json.RawMessage, error)
- func YAMLMatcher(path string) bool
- func YAMLToJSON(data interface{}) (json.RawMessage, error)
- type CommandLineOptionsGroup
- type JSONMapItem
- type JSONMapSlice
- type NameProvider
- func (n *NameProvider) GetGoName(subject interface{}, name string) (string, bool)
- func (n *NameProvider) GetGoNameForType(tpe reflect.Type, name string) (string, bool)
- func (n *NameProvider) GetJSONName(subject interface{}, name string) (string, bool)
- func (n *NameProvider) GetJSONNameForType(tpe reflect.Type, name string) (string, bool)
- func (n *NameProvider) GetJSONNames(subject interface{}) []string
Constants ¶
const (
// GOPATHKey represents the env key for gopath
GOPATHKey = "GOPATH"
)
Variables ¶
var DefaultJSONNameProvider = NewNameProvider()
DefaultJSONNameProvider the default cache for types
GoNamePrefixFunc sets an optional rule to prefix go names which do not start with a letter.
e.g. to help converting "123" into "{prefix}123"
The default is to prefix with "X"
LoadHTTPTimeout the default timeout for load requests
Functions ¶
func AddInitialisms ¶
func AddInitialisms(words ...string)
AddInitialisms add additional initialisms
func BoolMap ¶
BoolMap converts a string map of bool values into a string map of bool pointers
func BoolSlice ¶
BoolSlice converts a slice of bool values into a slice of bool pointers
func BoolValue ¶
BoolValue returns the value of the bool pointer passed in or false if the pointer is nil.
func BoolValueMap ¶
BoolValueMap converts a string map of bool pointers into a string map of bool values
func BoolValueSlice ¶
BoolValueSlice converts a slice of bool pointers into a slice of bool values
func BytesToYAMLDoc ¶
BytesToYAMLDoc converts a byte slice into a YAML document
func ConcatJSON ¶
ConcatJSON concatenates multiple json objects efficiently
func ContainsStrings ¶
ContainsStrings searches a slice of strings for a case-sensitive match
func ContainsStringsCI ¶
ContainsStringsCI searches a slice of strings for a case-insensitive match
func ConvertBool ¶
ConvertBool turn a string into a boolean
func ConvertFloat32 ¶
ConvertFloat32 turn a string into a float32
func ConvertFloat64 ¶
ConvertFloat64 turn a string into a float64
func ConvertInt16 ¶
ConvertInt16 turn a string into a int16
func ConvertInt32 ¶
ConvertInt32 turn a string into a int32
func ConvertInt64 ¶
ConvertInt64 turn a string into a int64
func ConvertInt8 ¶
ConvertInt8 turn a string into int8 boolean
func ConvertUint16 ¶
ConvertUint16 turn a string into a uint16
func ConvertUint32 ¶
ConvertUint32 turn a string into a uint32
func ConvertUint64 ¶
ConvertUint64 turn a string into a uint64
func ConvertUint8 ¶
ConvertUint8 turn a string into a uint8
func DynamicJSONToStruct ¶
func DynamicJSONToStruct(data interface{}, target interface{}) error
DynamicJSONToStruct converts an untyped json structure into a struct
func FindInGoSearchPath ¶
FindInGoSearchPath finds a package in the $GOPATH:$GOROOT
func FindInSearchPath ¶
FindInSearchPath finds a package in a provided lists of paths
func Float64 ¶
Float64 returns a pointer to of the float64 value passed in.
func Float64Map ¶
Float64Map converts a string map of float64 values into a string map of float64 pointers
func Float64Slice ¶
Float64Slice converts a slice of float64 values into a slice of float64 pointers
func Float64Value ¶
Float64Value returns the value of the float64 pointer passed in or 0 if the pointer is nil.
func Float64ValueMap ¶
Float64ValueMap converts a string map of float64 pointers into a string map of float64 values
func Float64ValueSlice ¶
Float64ValueSlice converts a slice of float64 pointers into a slice of float64 values
func FormatFloat32 ¶
FormatFloat32 turns a float32 into a string
func FormatFloat64 ¶
FormatFloat64 turns a float64 into a string
func FormatUint16 ¶
FormatUint16 turns an uint16 into a string
func FormatUint32 ¶
FormatUint32 turns an uint32 into a string
func FormatUint64 ¶
FormatUint64 turns an uint64 into a string
func FromDynamicJSON ¶
func FromDynamicJSON(data, target interface{}) error
FromDynamicJSON turns an object into a properly JSON typed structure
func FullGoSearchPath ¶
func FullGoSearchPath() string
FullGoSearchPath gets the search paths for finding packages
func Int32Map ¶
Int32Map converts a string map of int32 values into a string map of int32 pointers
func Int32Slice ¶
Int32Slice converts a slice of int64 values into a slice of int32 pointers
func Int32Value ¶
Int32Value returns the value of the int64 pointer passed in or 0 if the pointer is nil.
func Int32ValueMap ¶
Int32ValueMap converts a string map of int32 pointers into a string map of int32 values
func Int32ValueSlice ¶
Int32ValueSlice converts a slice of int32 pointers into a slice of int32 values
func Int64Map ¶
Int64Map converts a string map of int64 values into a string map of int64 pointers
func Int64Slice ¶
Int64Slice converts a slice of int64 values into a slice of int64 pointers
func Int64Value ¶
Int64Value returns the value of the int64 pointer passed in or 0 if the pointer is nil.
func Int64ValueMap ¶
Int64ValueMap converts a string map of int64 pointers into a string map of int64 values
func Int64ValueSlice ¶
Int64ValueSlice converts a slice of int64 pointers into a slice of int64 values
func IntMap ¶
IntMap converts a string map of int values into a string map of int pointers
func IntSlice ¶
IntSlice converts a slice of int values into a slice of int pointers
func IntValue ¶
IntValue returns the value of the int pointer passed in or 0 if the pointer is nil.
func IntValueMap ¶
IntValueMap converts a string map of int pointers into a string map of int values
func IntValueSlice ¶
IntValueSlice converts a slice of int pointers into a slice of int values
func IsFloat64AJSONInteger ¶
IsFloat64AJSONInteger allow for integers [-2^53, 2^53-1] inclusive
func IsZero ¶
func IsZero(data interface{}) bool
IsZero returns true when the value passed into the function is a zero value. This allows for safer checking of interface values.
func JoinByFormat ¶
JoinByFormat joins a string array by a known format (e.g. swagger's collectionFormat attribute):
ssv: space separated value tsv: tab separated value pipes: pipe (|) separated value csv: comma separated value (default)
func LoadFromFileOrHTTP ¶
LoadFromFileOrHTTP loads the bytes from a file or a remote http server based on the path passed in
func LoadFromFileOrHTTPWithTimeout ¶
LoadFromFileOrHTTPWithTimeout loads the bytes from a file or a remote http server based on the path passed in timeout arg allows for per request overriding of the request timeout
func LoadStrategy ¶
func LoadStrategy(path string, local, remote func(string) ([]byte, error)) func(string) ([]byte, error)
LoadStrategy returns a loader function for a given path or uri
func ReadJSON ¶
ReadJSON reads json data, prefers finding an appropriate interface to short-circuit the unmarshaller so it takes the fastes option available
func SplitByFormat ¶
SplitByFormat splits a string by a known format:
ssv: space separated value tsv: tab separated value pipes: pipe (|) separated value csv: comma separated value (default)
func SplitHostPort ¶
SplitHostPort splits a network address into a host and a port. The port is -1 when there is no port to be found
func String ¶
String returns a pointer to of the string value passed in.
func StringMap ¶
StringMap converts a string map of string values into a string map of string pointers
func StringSlice ¶
StringSlice converts a slice of string values into a slice of string pointers
func StringValue ¶
StringValue returns the value of the string pointer passed in or "" if the pointer is nil.
func StringValueMap ¶
StringValueMap converts a string map of string pointers into a string map of string values
func StringValueSlice ¶
StringValueSlice converts a slice of string pointers into a slice of string values
func Time ¶
Time returns a pointer to of the time.Time value passed in.
func TimeMap ¶
TimeMap converts a string map of time.Time values into a string map of time.Time pointers
func TimeSlice ¶
TimeSlice converts a slice of time.Time values into a slice of time.Time pointers
func TimeValue ¶
TimeValue returns the value of the time.Time pointer passed in or time.Time{} if the pointer is nil.
func TimeValueMap ¶
TimeValueMap converts a string map of time.Time pointers into a string map of time.Time values
func TimeValueSlice ¶
TimeValueSlice converts a slice of time.Time pointers into a slice of time.Time values
func ToCommandName ¶
ToCommandName lowercases and underscores a go type name
func ToDynamicJSON ¶
func ToDynamicJSON(data interface{}) interface{}
ToDynamicJSON turns an object into a properly JSON typed structure
func ToFileName ¶
ToFileName lowercases and underscores a go type name
func ToGoName ¶
ToGoName translates a swagger name which can be underscored or camel cased to a name that golint likes
func ToHumanNameLower ¶
ToHumanNameLower represents a code name as a human series of words
func ToHumanNameTitle ¶
ToHumanNameTitle represents a code name as a human series of words with the first letters titleized
func ToJSONName ¶
ToJSONName camelcases a name which can be underscored or pascal cased
func ToVarName ¶
ToVarName camelcases a name which can be underscored or pascal cased
func Uint32 ¶
Uint32 returns a pouinter to of the uint64 value passed in.
func Uint32Map ¶
Uint32Map converts a string map of uint32 values uinto a string map of uint32 pouinters
func Uint32Slice ¶
Uint32Slice converts a slice of uint64 values uinto a slice of uint32 pouinters
func Uint32Value ¶
Uint32Value returns the value of the uint64 pouinter passed in or 0 if the pouinter is nil.
func Uint32ValueMap ¶
Uint32ValueMap converts a string map of uint32 pouinters uinto a string map of uint32 values
func Uint32ValueSlice ¶
Uint32ValueSlice converts a slice of uint32 pouinters uinto a slice of uint32 values
func Uint64 ¶
Uint64 returns a pouinter to of the uint64 value passed in.
func Uint64Map ¶
Uint64Map converts a string map of uint64 values uinto a string map of uint64 pouinters
func Uint64Slice ¶
Uint64Slice converts a slice of uint64 values uinto a slice of uint64 pouinters
func Uint64Value ¶
Uint64Value returns the value of the uint64 pouinter passed in or 0 if the pouinter is nil.
func Uint64ValueMap ¶
Uint64ValueMap converts a string map of uint64 pouinters uinto a string map of uint64 values
func Uint64ValueSlice ¶
Uint64ValueSlice converts a slice of uint64 pouinters uinto a slice of uint64 values
func UintMap ¶
UintMap converts a string map of uint values uinto a string map of uint pouinters
func UintSlice ¶
UintSlice converts a slice of uint values uinto a slice of uint pouinters
func UintValue ¶
UintValue returns the value of the uint pouinter passed in or 0 if the pouinter is nil.
func UintValueMap ¶
UintValueMap converts a string map of uint pouinters uinto a string map of uint values
func UintValueSlice ¶
UintValueSlice converts a slice of uint pouinters uinto a slice of uint values
func WriteJSON ¶
WriteJSON writes json data, prefers finding an appropriate interface to short-circuit the marshaller so it takes the fastest option available.
func YAMLData ¶
YAMLData loads a yaml document from either http or a file
func YAMLDoc ¶
func YAMLDoc(path string) (json.RawMessage, error)
YAMLDoc loads a yaml document from either http or a file and converts it to json
func YAMLToJSON ¶
func YAMLToJSON(data interface{}) (json.RawMessage, error)
YAMLToJSON converts YAML unmarshaled data into json compatible data
Types ¶
type CommandLineOptionsGroup ¶
type CommandLineOptionsGroup struct { ShortDescription string LongDescription string Options interface{} }
CommandLineOptionsGroup represents a group of user-defined command line options
type JSONMapItem ¶
type JSONMapItem struct { Key string Value interface{} }
JSONMapItem represents the value of a key in a JSON object held by JSONMapSlice
func (JSONMapItem) MarshalEasyJSON ¶
func (s JSONMapItem) MarshalEasyJSON(w *jwriter.Writer)
MarshalEasyJSON renders a JSONMapItem as JSON, using easyJSON
func (JSONMapItem) MarshalJSON ¶
func (s JSONMapItem) MarshalJSON() ([]byte, error)
MarshalJSON renders a JSONMapItem as JSON
func (*JSONMapItem) UnmarshalEasyJSON ¶
func (s *JSONMapItem) UnmarshalEasyJSON(in *jlexer.Lexer)
UnmarshalEasyJSON makes a JSONMapItem from JSON, using easyJSON
func (*JSONMapItem) UnmarshalJSON ¶
func (s *JSONMapItem) UnmarshalJSON(data []byte) error
UnmarshalJSON makes a JSONMapItem from JSON
type JSONMapSlice ¶
type JSONMapSlice []JSONMapItem
JSONMapSlice represent a JSON object, with the order of keys maintained
func (JSONMapSlice) MarshalEasyJSON ¶
func (s JSONMapSlice) MarshalEasyJSON(w *jwriter.Writer)
MarshalEasyJSON renders a JSONMapSlice as JSON, using easyJSON
func (JSONMapSlice) MarshalJSON ¶
func (s JSONMapSlice) MarshalJSON() ([]byte, error)
MarshalJSON renders a JSONMapSlice as JSON
func (*JSONMapSlice) UnmarshalEasyJSON ¶
func (s *JSONMapSlice) UnmarshalEasyJSON(in *jlexer.Lexer)
UnmarshalEasyJSON makes a JSONMapSlice from JSON, using easyJSON
func (*JSONMapSlice) UnmarshalJSON ¶
func (s *JSONMapSlice) UnmarshalJSON(data []byte) error
UnmarshalJSON makes a JSONMapSlice from JSON
type NameProvider ¶
type NameProvider struct {
// contains filtered or unexported fields
}
NameProvider represents an object capabale of translating from go property names to json property names This type is thread-safe.
func NewNameProvider ¶
func NewNameProvider() *NameProvider
NewNameProvider creates a new name provider
func (*NameProvider) GetGoName ¶
func (n *NameProvider) GetGoName(subject interface{}, name string) (string, bool)
GetGoName gets the go name for a json property name
func (*NameProvider) GetGoNameForType ¶
GetGoNameForType gets the go name for a given type for a json property name
func (*NameProvider) GetJSONName ¶
func (n *NameProvider) GetJSONName(subject interface{}, name string) (string, bool)
GetJSONName gets the json name for a go property name
func (*NameProvider) GetJSONNameForType ¶
GetJSONNameForType gets the json name for a go property name on a given type
func (*NameProvider) GetJSONNames ¶
func (n *NameProvider) GetJSONNames(subject interface{}) []string
GetJSONNames gets all the json property names for a type