util

package
v1.11.1 Latest Latest
Warning

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

Go to latest
Published: Feb 2, 2021 License: Apache-2.0 Imports: 13 Imported by: 9

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateLogDirectory added in v1.4.0

func CreateLogDirectory(logPath string)

Checks if log directory exists. It creates the log directory, if it doesn't exist.

func Error added in v1.4.0

func Error(msg string, args ...interface{})

Prints an error message.

func Fatal added in v1.4.0

func Fatal(msg string, args ...interface{})

Prints a fatal error message and exits.

func Info added in v1.4.0

func Info(msg string, args ...interface{})

Prints an info message.

func MarshalByMethod

func MarshalByMethod(v interface{}, method string) ([]byte, error)

MarshalByMethod marshals any json tagged struct fields matching the method being specified.

If the `methods:` tag is specified for the field, then the field is marshaled if the input method is present in the comma-separated list within the tag (case insensitive).

If no `methods:` tag is present then it is presumed that the field is valid for all methods, so the field is marshaled.

Example:

```go
type Model struct {
	// Marshal this field for PATCH or POST methods
	A string `json:"a" methods:"PATCH,POST"`
	// Never marshal this field (no methods)
	B string `json:"b" methods:""`
	// Always marshal this field (all methods)
	C string `json:"c"`
	// Marshal this field for POST methods but only if value is not empty
	D string `json:"d,omitempty" methods:"POST"`
}
model := Model{
	A: "valueA",
	B: "valueB",
	D: "",
}
// A is marshaled because it matches the POST method
// B is not marshaled because the `methods:""` tag prevents marshaling for all methods
// C is marshaled because omitting the `methods:` tag always marshals the field
// D is not marshaled because even though the POST method matches the tag, the value is empty
//   and omitempty is specified
bytes, err := MarshalByMethod(model, "POST") // string(bytes) == `{"a":"valueA","c":""}`
```

func ParseHTTPStatusCodeInResponse

func ParseHTTPStatusCodeInResponse(response *http.Response) (*http.Response, error)

ParseHTTPStatusCodeInResponse returns http response and HTTPError struct based on response status code

func ParseResponse

func ParseResponse(model interface{}, response *http.Response) error

ParseResponse parses json-formatted http response and decodes it into pre-defined models

func ParseTemplatedPath

func ParseTemplatedPath(template string, path string) (map[string]string, error)

ParseTemplatedPath parses a url-like path using an input template and outputs a map of matched values. template should place the named params to be extracted in single braces, e.g. ParseTemplatedPath("{my_param1}/literal/path/{my_param2}/parts", "foo/literal/path/bar/parts") returns {"my_param1": "foo", "my_param2": "bar"}

func ParseURLParams

func ParseURLParams(model interface{}) url.Values

ParseURLParams parses a struct into url params based on its "key" tag It parses basic values and slices, and will parse structs recursively see https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#style-examples for details about style and explode formatting

func Warning added in v1.4.0

func Warning(msg string, args ...interface{})

Prints a warning message.

Types

type Credential

type Credential struct {
	// contains filtered or unexported fields
}

Credential is a simple string whose value is redacted when converted to a string via the Stringer interface in order to prevent accidental logging or other unintentional disclosure - value is retrieved using ClearText() method

func NewCredential

func NewCredential(s string) *Credential

NewCredential creates a Credential from a simple string

func (*Credential) ClearText

func (c *Credential) ClearText() string

ClearText returns the actual cleartext string value

func (*Credential) String

func (c *Credential) String() string

String returns a redacted string

type HTTPError

type HTTPError struct {
	HTTPStatusCode int
	HTTPStatus     string
	Message        string      `json:"message,omitempty"`
	Code           string      `json:"code,omitempty"`
	MoreInfo       string      `json:"moreInfo,omitempty"`
	Details        interface{} `json:"details,omitempty"`
}

HTTPError is raised when status code is not 2xx

func (*HTTPError) Error

func (he *HTTPError) Error() string

This allows HTTPError to satisfy the error interface

type Logger

type Logger interface {
	Print(v ...interface{})
}

Logger compatible with standard "log" library

type MethodMarshaler

type MethodMarshaler interface {
	MarshalJSONByMethod(method string) ([]byte, error)
}

MethodMarshaler is the interface implemented by types that can marshal themselves into valid JSON according to the input http method

type OpenAPIParameterStyle

type OpenAPIParameterStyle string
const (
	// StyleForm corresponds to style: form which is the default for query parameters
	StyleForm OpenAPIParameterStyle = "form"
)

type SdkTransport

type SdkTransport struct {
	// contains filtered or unexported fields
}

SdkTransport is to define a transport RoundTripper with user-defined logger

func NewCustomSdkTransport

func NewCustomSdkTransport(logger Logger, transport *http.Transport) *SdkTransport

NewCustomSdkTransport Creates a RoundTripper with user defined logger and http.Transport

func NewSdkTransport

func NewSdkTransport(logger Logger) *SdkTransport

NewSdkTransport Creates a RoundTripper with user defined logger

func (*SdkTransport) RoundTrip

func (st *SdkTransport) RoundTrip(request *http.Request) (*http.Response, error)

RoundTrip implements the RoundTripper interface

type Ticker

type Ticker struct {
	// contains filtered or unexported fields
}

Ticker is a wrapper of time.Ticker with additional functionality

func NewTicker

func NewTicker(duration time.Duration) *Ticker

NewTicker spits out a pointer to Ticker model. It sets ticker to stop state by default

func (*Ticker) GetChan

func (t *Ticker) GetChan() <-chan time.Time

GetChan returns the channel from ticker

func (*Ticker) IsRunning

func (t *Ticker) IsRunning() bool

IsRunning returns bool indicating whether or not ticker is running

func (*Ticker) Reset

func (t *Ticker) Reset()

Reset resets ticker

func (*Ticker) Start

func (t *Ticker) Start()

Start starts a new ticker and set property running to true

func (*Ticker) Stop

func (t *Ticker) Stop()

Stop stops ticker and set property running to false

Jump to

Keyboard shortcuts

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