swag

package module
v0.23.0 Latest Latest
Warning

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

Go to latest
Published: Mar 9, 2024 License: Apache-2.0 Imports: 27 Imported by: 11,388

README

Swag Build Status codecov

Slack Status license Go Reference Go Report Card

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.v3
  • github.com/mailru/easyjson v0.7.7

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

View Source
const (
	// GOPATHKey represents the env key for gopath
	GOPATHKey = "GOPATH"
)

Variables

View Source
var DefaultJSONNameProvider = NewNameProvider()

DefaultJSONNameProvider the default cache for types

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

View Source
var LoadHTTPBasicAuthPassword = ""

LoadHTTPBasicAuthPassword the password to use when load requests require basic auth

View Source
var LoadHTTPBasicAuthUsername = ""

LoadHTTPBasicAuthUsername the username to use when load requests require basic auth

View Source
var LoadHTTPCustomHeaders = map[string]string{}

LoadHTTPCustomHeaders an optional collection of custom HTTP headers for load requests

View Source
var LoadHTTPTimeout = 30 * time.Second

LoadHTTPTimeout the default timeout for load requests

Functions

func AddInitialisms

func AddInitialisms(words ...string)

AddInitialisms add additional initialisms

func Bool

func Bool(v bool) *bool

Bool returns a pointer to of the bool value passed in.

func BoolMap

func BoolMap(src map[string]bool) map[string]*bool

BoolMap converts a string map of bool values into a string map of bool pointers

func BoolSlice

func BoolSlice(src []bool) []*bool

BoolSlice converts a slice of bool values into a slice of bool pointers

func BoolValue

func BoolValue(v *bool) bool

BoolValue returns the value of the bool pointer passed in or false if the pointer is nil.

func BoolValueMap

func BoolValueMap(src map[string]*bool) map[string]bool

BoolValueMap converts a string map of bool pointers into a string map of bool values

func BoolValueSlice

func BoolValueSlice(src []*bool) []bool

BoolValueSlice converts a slice of bool pointers into a slice of bool values

func BytesToYAMLDoc

func BytesToYAMLDoc(data []byte) (interface{}, error)

BytesToYAMLDoc converts a byte slice into a YAML document

func Camelize

func Camelize(word string) string

Camelize an uppercased word

func ConcatJSON

func ConcatJSON(blobs ...[]byte) []byte

ConcatJSON concatenates multiple json objects efficiently

func ContainsStrings

func ContainsStrings(coll []string, item string) bool

ContainsStrings searches a slice of strings for a case-sensitive match

func ContainsStringsCI

func ContainsStringsCI(coll []string, item string) bool

ContainsStringsCI searches a slice of strings for a case-insensitive match

func ConvertBool

func ConvertBool(str string) (bool, error)

ConvertBool turn a string into a boolean

func ConvertFloat32

func ConvertFloat32(str string) (float32, error)

ConvertFloat32 turn a string into a float32

func ConvertFloat64

func ConvertFloat64(str string) (float64, error)

ConvertFloat64 turn a string into a float64

func ConvertInt16

func ConvertInt16(str string) (int16, error)

ConvertInt16 turn a string into an int16

func ConvertInt32

func ConvertInt32(str string) (int32, error)

ConvertInt32 turn a string into an int32

func ConvertInt64

func ConvertInt64(str string) (int64, error)

ConvertInt64 turn a string into an int64

func ConvertInt8

func ConvertInt8(str string) (int8, error)

ConvertInt8 turn a string into an int8

func ConvertUint16

func ConvertUint16(str string) (uint16, error)

ConvertUint16 turn a string into an uint16

func ConvertUint32

func ConvertUint32(str string) (uint32, error)

ConvertUint32 turn a string into an uint32

func ConvertUint64

func ConvertUint64(str string) (uint64, error)

ConvertUint64 turn a string into an uint64

func ConvertUint8

func ConvertUint8(str string) (uint8, error)

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

func FindInGoSearchPath(pkg string) string

FindInGoSearchPath finds a package in the $GOPATH:$GOROOT

func FindInSearchPath

func FindInSearchPath(searchPath, pkg string) string

FindInSearchPath finds a package in a provided lists of paths

func Float32 added in v0.19.9

func Float32(v float32) *float32

Float32 returns a pointer to of the float32 value passed in.

func Float32Map added in v0.19.9

func Float32Map(src map[string]float32) map[string]*float32

Float32Map converts a string map of float32 values into a string map of float32 pointers

func Float32Slice added in v0.19.9

func Float32Slice(src []float32) []*float32

Float32Slice converts a slice of float32 values into a slice of float32 pointers

func Float32Value added in v0.19.9

func Float32Value(v *float32) float32

Float32Value returns the value of the float32 pointer passed in or 0 if the pointer is nil.

func Float32ValueMap added in v0.19.9

func Float32ValueMap(src map[string]*float32) map[string]float32

Float32ValueMap converts a string map of float32 pointers into a string map of float32 values

func Float32ValueSlice added in v0.19.9

func Float32ValueSlice(src []*float32) []float32

Float32ValueSlice converts a slice of float32 pointers into a slice of float32 values

func Float64

func Float64(v float64) *float64

Float64 returns a pointer to of the float64 value passed in.

func Float64Map

func Float64Map(src map[string]float64) map[string]*float64

Float64Map converts a string map of float64 values into a string map of float64 pointers

func Float64Slice

func Float64Slice(src []float64) []*float64

Float64Slice converts a slice of float64 values into a slice of float64 pointers

func Float64Value

func Float64Value(v *float64) float64

Float64Value returns the value of the float64 pointer passed in or 0 if the pointer is nil.

func Float64ValueMap

func Float64ValueMap(src map[string]*float64) map[string]float64

Float64ValueMap converts a string map of float64 pointers into a string map of float64 values

func Float64ValueSlice

func Float64ValueSlice(src []*float64) []float64

Float64ValueSlice converts a slice of float64 pointers into a slice of float64 values

func FormatBool

func FormatBool(value bool) string

FormatBool turns a boolean into a string

func FormatFloat32

func FormatFloat32(value float32) string

FormatFloat32 turns a float32 into a string

func FormatFloat64

func FormatFloat64(value float64) string

FormatFloat64 turns a float64 into a string

func FormatInt16

func FormatInt16(value int16) string

FormatInt16 turns an int16 into a string

func FormatInt32

func FormatInt32(value int32) string

FormatInt32 turns an int32 into a string

func FormatInt64

func FormatInt64(value int64) string

FormatInt64 turns an int64 into a string

func FormatInt8

func FormatInt8(value int8) string

FormatInt8 turns an int8 into a string

func FormatUint16

func FormatUint16(value uint16) string

FormatUint16 turns an uint16 into a string

func FormatUint32

func FormatUint32(value uint32) string

FormatUint32 turns an uint32 into a string

func FormatUint64

func FormatUint64(value uint64) string

FormatUint64 turns an uint64 into a string

func FormatUint8

func FormatUint8(value uint8) string

FormatUint8 turns an uint8 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 Int

func Int(v int) *int

Int returns a pointer to of the int value passed in.

func Int32

func Int32(v int32) *int32

Int32 returns a pointer to of the int32 value passed in.

func Int32Map

func Int32Map(src map[string]int32) map[string]*int32

Int32Map converts a string map of int32 values into a string map of int32 pointers

func Int32Slice

func Int32Slice(src []int32) []*int32

Int32Slice converts a slice of int32 values into a slice of int32 pointers

func Int32Value

func Int32Value(v *int32) int32

Int32Value returns the value of the int32 pointer passed in or 0 if the pointer is nil.

func Int32ValueMap

func Int32ValueMap(src map[string]*int32) map[string]int32

Int32ValueMap converts a string map of int32 pointers into a string map of int32 values

func Int32ValueSlice

func Int32ValueSlice(src []*int32) []int32

Int32ValueSlice converts a slice of int32 pointers into a slice of int32 values

func Int64

func Int64(v int64) *int64

Int64 returns a pointer to of the int64 value passed in.

func Int64Map

func Int64Map(src map[string]int64) map[string]*int64

Int64Map converts a string map of int64 values into a string map of int64 pointers

func Int64Slice

func Int64Slice(src []int64) []*int64

Int64Slice converts a slice of int64 values into a slice of int64 pointers

func Int64Value

func Int64Value(v *int64) int64

Int64Value returns the value of the int64 pointer passed in or 0 if the pointer is nil.

func Int64ValueMap

func Int64ValueMap(src map[string]*int64) map[string]int64

Int64ValueMap converts a string map of int64 pointers into a string map of int64 values

func Int64ValueSlice

func Int64ValueSlice(src []*int64) []int64

Int64ValueSlice converts a slice of int64 pointers into a slice of int64 values

func IntMap

func IntMap(src map[string]int) map[string]*int

IntMap converts a string map of int values into a string map of int pointers

func IntSlice

func IntSlice(src []int) []*int

IntSlice converts a slice of int values into a slice of int pointers

func IntValue

func IntValue(v *int) int

IntValue returns the value of the int pointer passed in or 0 if the pointer is nil.

func IntValueMap

func IntValueMap(src map[string]*int) map[string]int

IntValueMap converts a string map of int pointers into a string map of int values

func IntValueSlice

func IntValueSlice(src []*int) []int

IntValueSlice converts a slice of int pointers into a slice of int values

func IsFloat64AJSONInteger

func IsFloat64AJSONInteger(f float64) bool

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

func JoinByFormat(data []string, format string) []string

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

func LoadFromFileOrHTTP(pth string) ([]byte, error)

LoadFromFileOrHTTP loads the bytes from a file or a remote http server based on the path passed in

func LoadFromFileOrHTTPWithTimeout

func LoadFromFileOrHTTPWithTimeout(pth string, timeout time.Duration) ([]byte, error)

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

func ReadJSON(data []byte, value interface{}) error

ReadJSON reads json data, prefers finding an appropriate interface to short-circuit the unmarshaler so it takes the fastest option available

func SplitByFormat

func SplitByFormat(data, format string) []string

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

func SplitHostPort(addr string) (host string, port int, err error)

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

func String(v string) *string

String returns a pointer to of the string value passed in.

func StringMap

func StringMap(src map[string]string) map[string]*string

StringMap converts a string map of string values into a string map of string pointers

func StringSlice

func StringSlice(src []string) []*string

StringSlice converts a slice of string values into a slice of string pointers

func StringValue

func StringValue(v *string) string

StringValue returns the value of the string pointer passed in or "" if the pointer is nil.

func StringValueMap

func StringValueMap(src map[string]*string) map[string]string

StringValueMap converts a string map of string pointers into a string map of string values

func StringValueSlice

func StringValueSlice(src []*string) []string

StringValueSlice converts a slice of string pointers into a slice of string values

func Time

func Time(v time.Time) *time.Time

Time returns a pointer to of the time.Time value passed in.

func TimeMap

func TimeMap(src map[string]time.Time) map[string]*time.Time

TimeMap converts a string map of time.Time values into a string map of time.Time pointers

func TimeSlice

func TimeSlice(src []time.Time) []*time.Time

TimeSlice converts a slice of time.Time values into a slice of time.Time pointers

func TimeValue

func TimeValue(v *time.Time) time.Time

TimeValue returns the value of the time.Time pointer passed in or time.Time{} if the pointer is nil.

func TimeValueMap

func TimeValueMap(src map[string]*time.Time) map[string]time.Time

TimeValueMap converts a string map of time.Time pointers into a string map of time.Time values

func TimeValueSlice

func TimeValueSlice(src []*time.Time) []time.Time

TimeValueSlice converts a slice of time.Time pointers into a slice of time.Time values

func ToCommandName

func ToCommandName(name string) string

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

func ToFileName(name string) string

ToFileName lowercases and underscores a go type name

func ToGoName

func ToGoName(name string) string

ToGoName translates a swagger name which can be underscored or camel cased to a name that golint likes

func ToHumanNameLower

func ToHumanNameLower(name string) string

ToHumanNameLower represents a code name as a human series of words

func ToHumanNameTitle

func ToHumanNameTitle(name string) string

ToHumanNameTitle represents a code name as a human series of words with the first letters titleized

func ToJSONName

func ToJSONName(name string) string

ToJSONName camelcases a name which can be underscored or pascal cased

func ToVarName

func ToVarName(name string) string

ToVarName camelcases a name which can be underscored or pascal cased

func Uint

func Uint(v uint) *uint

Uint returns a pointer to of the uint value passed in.

func Uint16 added in v0.19.9

func Uint16(v uint16) *uint16

Uint16 returns a pointer to of the uint16 value passed in.

func Uint16Map added in v0.19.9

func Uint16Map(src map[string]uint16) map[string]*uint16

Uint16Map converts a string map of uint16 values into a string map of uint16 pointers

func Uint16Slice added in v0.19.9

func Uint16Slice(src []uint16) []*uint16

Uint16Slice converts a slice of uint16 values into a slice of uint16 pointers

func Uint16Value added in v0.19.9

func Uint16Value(v *uint16) uint16

Uint16Value returns the value of the uint16 pointer passed in or 0 if the pointer is nil.

func Uint16ValueMap added in v0.19.9

func Uint16ValueMap(src map[string]*uint16) map[string]uint16

Uint16ValueMap converts a string map of uint16 pointers into a string map of uint16 values

func Uint16ValueSlice added in v0.19.9

func Uint16ValueSlice(src []*uint16) []uint16

Uint16ValueSlice converts a slice of uint16 pointers into a slice of uint16 values

func Uint32

func Uint32(v uint32) *uint32

Uint32 returns a pointer to of the uint32 value passed in.

func Uint32Map

func Uint32Map(src map[string]uint32) map[string]*uint32

Uint32Map converts a string map of uint32 values into a string map of uint32 pointers

func Uint32Slice

func Uint32Slice(src []uint32) []*uint32

Uint32Slice converts a slice of uint32 values into a slice of uint32 pointers

func Uint32Value

func Uint32Value(v *uint32) uint32

Uint32Value returns the value of the uint32 pointer passed in or 0 if the pointer is nil.

func Uint32ValueMap

func Uint32ValueMap(src map[string]*uint32) map[string]uint32

Uint32ValueMap converts a string map of uint32 pointers into a string map of uint32 values

func Uint32ValueSlice

func Uint32ValueSlice(src []*uint32) []uint32

Uint32ValueSlice converts a slice of uint32 pointers into a slice of uint32 values

func Uint64

func Uint64(v uint64) *uint64

Uint64 returns a pointer to of the uint64 value passed in.

func Uint64Map

func Uint64Map(src map[string]uint64) map[string]*uint64

Uint64Map converts a string map of uint64 values into a string map of uint64 pointers

func Uint64Slice

func Uint64Slice(src []uint64) []*uint64

Uint64Slice converts a slice of uint64 values into a slice of uint64 pointers

func Uint64Value

func Uint64Value(v *uint64) uint64

Uint64Value returns the value of the uint64 pointer passed in or 0 if the pointer is nil.

func Uint64ValueMap

func Uint64ValueMap(src map[string]*uint64) map[string]uint64

Uint64ValueMap converts a string map of uint64 pointers into a string map of uint64 values

func Uint64ValueSlice

func Uint64ValueSlice(src []*uint64) []uint64

Uint64ValueSlice converts a slice of uint64 pointers into a slice of uint64 values

func UintMap

func UintMap(src map[string]uint) map[string]*uint

UintMap converts a string map of uint values into a string map of uint pointers

func UintSlice

func UintSlice(src []uint) []*uint

UintSlice converts a slice of uint values into a slice of uint pointers

func UintValue

func UintValue(v *uint) uint

UintValue returns the value of the uint pointer passed in or 0 if the pointer is nil.

func UintValueMap

func UintValueMap(src map[string]*uint) map[string]uint

UintValueMap converts a string map of uint pointers into a string map of uint values

func UintValueSlice

func UintValueSlice(src []*uint) []uint

UintValueSlice converts a slice of uint pointers into a slice of uint values

func WriteJSON

func WriteJSON(data interface{}) ([]byte, error)

WriteJSON writes json data, prefers finding an appropriate interface to short-circuit the marshaler so it takes the fastest option available.

func YAMLData

func YAMLData(path string) (interface{}, error)

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 YAMLMatcher

func YAMLMatcher(path string) bool

YAMLMatcher matches yaml

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.

func (*File) Close added in v0.21.0

func (f *File) Close() error

Close the file

func (*File) Read added in v0.21.0

func (f *File) Read(p []byte) (n int, err error)

Read bytes from the 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

func (n *NameProvider) GetGoNameForType(tpe reflect.Type, name string) (string, bool)

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

func (n *NameProvider) GetJSONNameForType(tpe reflect.Type, name string) (string, bool)

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

Jump to

Keyboard shortcuts

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