common

package module
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Sep 16, 2022 License: Apache-2.0 Imports: 17 Imported by: 5

README

common-go

Common Go functionality shared across SWAN repositories.

Documentation

Overview

**************************************************************************** * Copyright 2022 51 Degrees Mobile Experts Limited (51degrees.com) * * Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations * under the License. * **************************************************************************

Index

Constants

This section is empty.

Variables

View Source
var IoDateBase = time.Date(2020, time.Month(1), 1, 0, 0, 0, 0, time.UTC)

The base year for all dates encoded with the io time methods.

Functions

func GetDateFromMinutes

func GetDateFromMinutes(t uint32) time.Time

GetTimeFromMinutes returns the date time from the minutes provided.

func GetDateInMinutes

func GetDateInMinutes(t time.Time) uint32

GetDateInMinutes returns the number of minutes that have elapsed since the IoDateBase epoch.

func GetWriter

func GetWriter(writer http.ResponseWriter, contentType string) *gzip.Writer

GetWriter creates a new compressed writer for the content type provided.

func HTTPTest

func HTTPTest(
	t *testing.T,
	method string,
	url *url.URL,
	body io.Reader,
	handler func(w http.ResponseWriter, r *http.Request)) *httptest.ResponseRecorder

HTTPTest returns a test response after having processed the handler, method, URL, and body provided. t testing instance method HTTP method host url HTTP url values query values body data handler HTTP handler being tested

func ReadBool

func ReadBool(b *bytes.Buffer) (bool, error)

Read the next byte as a bool and returns the value.

func ReadByte

func ReadByte(b *bytes.Buffer) (byte, error)

ReadByte reads the next byte from the buffer.

func ReadByteArray

func ReadByteArray(b *bytes.Buffer) ([]byte, error)

ReadByteArray reads the first 4 bytes as an unsigned 32 bit integer to determine the length of the byte array contained in the following bytes.

func ReadByteArrayArray

func ReadByteArrayArray(b *bytes.Buffer) ([][]byte, error)

ReadByteArrayArray from two dimensional array of bytes.

func ReadByteArrayNoLength

func ReadByteArrayNoLength(b *bytes.Buffer, l int) ([]byte, error)

ReadByteArrayNoLength reads the number of bytes specified into a new byte array.

func ReadDate

func ReadDate(b *bytes.Buffer) (time.Time, error)

ReadDate reads the date from the unsigned 16 bit integer and then determines the date by adding this to the IoDateBase epoch.

func ReadDateFromUInt32

func ReadDateFromUInt32(b *bytes.Buffer) (time.Time, error)

ReadDateFromUInt32 reads the date from the buffer where the date is stored as the number of minutes as an unsigned 32 bit integer that have elapsed since the IoDateBase epoch.

func ReadFloat32

func ReadFloat32(b *bytes.Buffer) (float32, error)

ReadFloat32 from the buffer.

func ReadMarshaller

func ReadMarshaller(b *bytes.Buffer, m encoding.BinaryUnmarshaler) error

ReadMarshaller reads the content into the unmarshaler instance.

func ReadString

func ReadString(b *bytes.Buffer) (string, error)

ReadString reads a null (zero) terminated string from the byte buffer.

func ReadStrings

func ReadStrings(b *bytes.Buffer) ([]string, error)

ReadStrings into one dimensional array of strings.

func ReadTime

func ReadTime(b *bytes.Buffer) (time.Time, error)

ReadTime from the next binary object.

func ReadUint16

func ReadUint16(b *bytes.Buffer) (uint16, error)

ReadUint16 reads an unsigned 16 bit integer from the buffer. The integer is stored in little endian format.

func ReadUint32

func ReadUint32(b *bytes.Buffer) (uint32, error)

ReadUint32 reads an unsigned 32 bit integer from the buffer. The integer is stored in little endian format.

func ResponseAsByteArrayTest added in v0.1.2

func ResponseAsByteArrayTest(
	t *testing.T,
	rr *httptest.ResponseRecorder) []byte

ResponseAsByteArrayTest decompresses the response as a byte array.

func ResponseAsMapTest added in v0.1.2

func ResponseAsMapTest(
	t *testing.T,
	rr *httptest.ResponseRecorder) map[string]interface{}

ResponseAsMapTest decompresses the response and returns the body as a map.

func ResponseAsStringTest added in v0.1.2

func ResponseAsStringTest(
	t *testing.T,
	rr *httptest.ResponseRecorder) string

ResponseAsStringTest decompresses the response and returns the body as a string.

func ReturnApplicationError

func ReturnApplicationError(writer http.ResponseWriter, err *HttpError)

ReturnApplicationError handles HTTP application errors consistently. writer for the response err details of the error

func ReturnError

func ReturnError(writer http.ResponseWriter, err *HttpError)

ReturnError handles all HTTP errors consistently. writer for the response err details of the error

func ReturnServerError

func ReturnServerError(writer http.ResponseWriter, err error)

ReturnServerError handles HTTP server errors consistently ensuring they are output to the logger. writer for the response message to be sent in the response err the error to be logged and included in the response if debug is true

func SendByteArray

func SendByteArray(writer http.ResponseWriter, data []byte)

SendByteArray writes the data as an octet-stream.

func SendByteArrayUncompressed

func SendByteArrayUncompressed(writer http.ResponseWriter, data []byte)

SendByteArrayUncompressed writes the data as an octet-stream without compression.

func SendHTMLTemplate

func SendHTMLTemplate(
	writer http.ResponseWriter,
	temp *template.Template,
	model interface{})

SendHTMLTemplate parses the template with the model provided and then outputs the result as HTML.

func SendJS

func SendJS(writer http.ResponseWriter, data []byte)

SendJS sends the JSON data provided.

func SendJSTemplate

func SendJSTemplate(
	writer http.ResponseWriter,
	temp *template.Template,
	model interface{})

SendJSTemplate parses the template with the model provided and then outputs the result as JS.

func SendResponse

func SendResponse(
	writer http.ResponseWriter,
	contentType string,
	data []byte,
	compress bool)

SendResponse writes out the data with the content type provided.

func SendString

func SendString(writer http.ResponseWriter, value string)

SendString writes out the string value with the appropriate content type.

func SendTemplate

func SendTemplate(
	writer http.ResponseWriter,
	temp *template.Template,
	contentType string,
	model interface{})

SendTemplate parses the template with the model provided and then outputs the result for the content type provided.

func TestCompareDate

func TestCompareDate(t *testing.T, a time.Time, b time.Time)

func WriteBool

func WriteBool(b *bytes.Buffer, v bool) error

WriteBool writes the boolean value to the buffer as a byte.

func WriteByte

func WriteByte(b *bytes.Buffer, i byte) error

WriteByte writes the byte provided to the buffer.

func WriteByteArray

func WriteByteArray(b *bytes.Buffer, v []byte) error

ReadByteArray writes the length of the byte array as an unsigned 32 bit integer followed by the bytes.

func WriteByteArrayArray

func WriteByteArrayArray(b *bytes.Buffer, v [][]byte) error

WriteByteArrayArray two dimensional array of bytes.

func WriteByteArrayNoLength

func WriteByteArrayNoLength(b *bytes.Buffer, v []byte) error

WriteByteArrayNoLength writes the byte array to the buffer without recording the length. Used with fixed length data.

func WriteDate

func WriteDate(b *bytes.Buffer, t time.Time) error

WriteDate writes the date as the number of hours since the IoDateBase epoch. Uses an unsigned 16 bit integer.

func WriteDateToUInt32

func WriteDateToUInt32(b *bytes.Buffer, t time.Time) error

WriteDateToUInt32 writes the date to the buffer as an unsigned 32 bit representing the number of minutes that have elapsed since the IoDateBase epoch.

func WriteFloat32

func WriteFloat32(b *bytes.Buffer, f float32) error

WriteFloat32 to the buffer.

func WriteMarshaller

func WriteMarshaller(b *bytes.Buffer, m encoding.BinaryMarshaler) error

WriteMarshaller writes the result of marshal binary call to the buffer.

func WriteString

func WriteString(b *bytes.Buffer, s string) error

WriteString writes a null (zero) terminated string to the byte buffer.

func WriteStrings

func WriteStrings(b *bytes.Buffer, v []string) error

WriteStrings one dimensional array of strings.

func WriteTime

func WriteTime(b *bytes.Buffer, t time.Time) error

WriteTime as a binary object.

func WriteUint16

func WriteUint16(b *bytes.Buffer, i uint16) error

WriteUint16 reads an unsigned 16 bit integer from the buffer where the integer is stored in little endian format.

func WriteUint32

func WriteUint32(b *bytes.Buffer, i uint32) error

WriteUint32 reads an unsigned 32 bit integer from the buffer where the integer is stored in little endian format.

Types

type HttpError

type HttpError struct {
	Request *http.Request // the HTTP request
	Log     bool          // true if the error should be written to the log
	Message string        // message to return in the HTTP response
	Code    int           // HTTP status code for the response
	Error   error         // the server error - never sent in the response
}

HttpError associated with HTTP handlers.

Jump to

Keyboard shortcuts

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