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) 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 Float32(v float32) *float32
- func Float32Map(src map[string]float32) map[string]*float32
- func Float32Slice(src []float32) []*float32
- func Float32Value(v *float32) float32
- func Float32ValueMap(src map[string]*float32) map[string]float32
- func Float32ValueSlice(src []*float32) []float32
- 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(pth string) ([]byte, error)
- func LoadFromFileOrHTTPWithTimeout(pth string, timeout time.Duration) ([]byte, error)
- func LoadStrategy(pth 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 Uint16(v uint16) *uint16
- func Uint16Map(src map[string]uint16) map[string]*uint16
- func Uint16Slice(src []uint16) []*uint16
- func Uint16Value(v *uint16) uint16
- func Uint16ValueMap(src map[string]*uint16) map[string]uint16
- func Uint16ValueSlice(src []*uint16) []uint16
- 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 File
- 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
var GoNamePrefixFunc func(string) string
GoNamePrefixFunc sets an optional rule to prefix go names which do not start with a letter.
The prefix function is assumed to return a string that starts with an upper case letter.
e.g. to help convert "123" into "{prefix}123"
The default is to prefix with "X"
var LoadHTTPBasicAuthPassword = ""
LoadHTTPBasicAuthPassword the password to use when load requests require basic auth
var LoadHTTPBasicAuthUsername = ""
LoadHTTPBasicAuthUsername the username to use when load requests require basic auth
var LoadHTTPCustomHeaders = map[string]string{}
LoadHTTPCustomHeaders an optional collection of custom HTTP headers for load requests
var LoadHTTPTimeout = 30 * time.Second
LoadHTTPTimeout the default timeout for load requests
Functions ¶
func AddInitialisms ¶
func AddInitialisms(words ...string)
AddInitialisms add additional initialisms
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 an int16
func ConvertInt32 ¶
ConvertInt32 turn a string into an int32
func ConvertInt64 ¶
ConvertInt64 turn a string into an int64
func ConvertInt8 ¶
ConvertInt8 turn a string into an int8
func ConvertUint16 ¶
ConvertUint16 turn a string into an uint16
func ConvertUint32 ¶
ConvertUint32 turn a string into an uint32
func ConvertUint64 ¶
ConvertUint64 turn a string into an uint64
func ConvertUint8 ¶
ConvertUint8 turn a string into an 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 Float32Map ¶ added in v0.19.9
Float32Map converts a string map of float32 values into a string map of float32 pointers
func Float32Slice ¶ added in v0.19.9
Float32Slice converts a slice of float32 values into a slice of float32 pointers
func Float32Value ¶ added in v0.19.9
Float32Value returns the value of the float32 pointer passed in or 0 if the pointer is nil.
func Float32ValueMap ¶ added in v0.19.9
Float32ValueMap converts a string map of float32 pointers into a string map of float32 values
func Float32ValueSlice ¶ added in v0.19.9
Float32ValueSlice converts a slice of float32 pointers into a slice of float32 values
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 Int32Slice ¶
Int32Slice converts a slice of int32 values into a slice of int32 pointers
func Int32Value ¶
Int32Value returns the value of the int32 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 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 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(pth string, local, remote func(string) ([]byte, error)) func(string) ([]byte, error)
LoadStrategy returns a loader function for a given path or URI.
The load strategy returns the remote load for any path starting with `http`. So this works for any URI with a scheme `http` or `https`.
The fallback strategy is to call the local loader.
The local loader takes a local file system path (absolute or relative) as argument, or alternatively a `file://...` URI, **without host** (see also below for windows).
There are a few liberalities, initially intended to be tolerant regarding the URI syntax, especially on windows.
Before the local loader is called, the given path is transformed:
- percent-encoded characters are unescaped
- simple paths (e.g. `./folder/file`) are passed as-is
- on windows, occurrences of `/` are replaced by `\`, so providing a relative path such a `folder/file` works too.
For paths provided as URIs with the "file" scheme, please note that:
- `file://` is simply stripped. This means that the host part of the URI is not parsed at all. For example, `file:///folder/file" becomes "/folder/file`, but `file://localhost/folder/file` becomes `localhost/folder/file` on unix systems. Similarly, `file://./folder/file` yields `./folder/file`.
- on windows, `file://...` can take a host so as to specify an UNC share location.
Reminder about windows-specifics: - `file://host/folder/file` becomes an UNC path like `\\host\folder\file` (no port specification is supported) - `file:///c:/folder/file` becomes `C:\folder\file` - `file://c:/folder/file` is tolerated (without leading `/`) and becomes `c:\folder\file`
func ReadJSON ¶
ReadJSON reads json data, prefers finding an appropriate interface to short-circuit the unmarshaler so it takes the fastest 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 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 TimeMap ¶
TimeMap converts a string map of time.Time values into a string map 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 Uint16Map ¶ added in v0.19.9
Uint16Map converts a string map of uint16 values into a string map of uint16 pointers
func Uint16Slice ¶ added in v0.19.9
Uint16Slice converts a slice of uint16 values into a slice of uint16 pointers
func Uint16Value ¶ added in v0.19.9
Uint16Value returns the value of the uint16 pointer passed in or 0 if the pointer is nil.
func Uint16ValueMap ¶ added in v0.19.9
Uint16ValueMap converts a string map of uint16 pointers into a string map of uint16 values
func Uint16ValueSlice ¶ added in v0.19.9
Uint16ValueSlice converts a slice of uint16 pointers into a slice of uint16 values
func Uint32Map ¶
Uint32Map converts a string map of uint32 values into a string map of uint32 pointers
func Uint32Slice ¶
Uint32Slice converts a slice of uint32 values into a slice of uint32 pointers
func Uint32Value ¶
Uint32Value returns the value of the uint32 pointer passed in or 0 if the pointer is nil.
func Uint32ValueMap ¶
Uint32ValueMap converts a string map of uint32 pointers into a string map of uint32 values
func Uint32ValueSlice ¶
Uint32ValueSlice converts a slice of uint32 pointers into a slice of uint32 values
func Uint64Map ¶
Uint64Map converts a string map of uint64 values into a string map of uint64 pointers
func Uint64Slice ¶
Uint64Slice converts a slice of uint64 values into a slice of uint64 pointers
func Uint64Value ¶
Uint64Value returns the value of the uint64 pointer passed in or 0 if the pointer is nil.
func Uint64ValueMap ¶
Uint64ValueMap converts a string map of uint64 pointers into a string map of uint64 values
func Uint64ValueSlice ¶
Uint64ValueSlice converts a slice of uint64 pointers into a slice of uint64 values
func UintValue ¶
UintValue returns the value of the uint pointer passed in or 0 if the pointer is nil.
func UintValueMap ¶
UintValueMap converts a string map of uint pointers into a string map of uint values
func UintValueSlice ¶
UintValueSlice converts a slice of uint pointers into a slice of uint values
func WriteJSON ¶
WriteJSON writes json data, prefers finding an appropriate interface to short-circuit the marshaler so it takes the fastest option available.
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 File ¶ added in v0.21.0
type File struct { Data multipart.File Header *multipart.FileHeader }
File represents an uploaded file.
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) MarshalYAML ¶ added in v0.22.1
func (s JSONMapSlice) MarshalYAML() (interface{}, error)
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 capable 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