common

package module
v2.0.1 Latest Latest
Warning

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

Go to latest
Published: Sep 22, 2021 License: MPL-2.0 Imports: 18 Imported by: 0

README

Common

Test Go DeepSource Go Report Card codecov

This repo contains common code that I use between my programs. There are currently some tests and at some point I might get around to writing more.

Tested with go versions 1.16 & 1.17.

Benchmark

Benchmarks are taken from latest-ubuntu and go version 1.16.

BenchmarkJSONResponse-2      	 1155928	      1016 ns/op	    1024 B/op	      10 allocs/op
BenchmarkWOAllowedMethod-2   	 1000000	      1039 ns/op	    1016 B/op	      10 allocs/op
BenchmarkAllowedMethod-2     	 1000000	      1206 ns/op	    1064 B/op	      12 allocs/op
BenchmarkContentResponse-2   	 1000000	      1021 ns/op	    1013 B/op	      10 allocs/op
BenchmarkStringResponse-2    	 1000000	      1040 ns/op	    1016 B/op	      10 allocs/op
BenchmarkJSONMarshall-2      	  933313	      1343 ns/op	    1024 B/op	      10 allocs/op
BenchmarkGenerate-2          	  614492	      1982 ns/op
BenchmarkJSONParse-2         	  106275	     11315 ns/op	    1096 B/op	      12 allocs/op
BenchmarkYAMLParse-2         	   49432	     23725 ns/op	    7616 B/op	      84 allocs/op
BenchmarkStringSearch2-2     	44019916	        26.47 ns/op
BenchmarkStringSearch10-2    	25348384	        46.87 ns/op
BenchmarkFloatSearch2-2      	99105530	        11.99 ns/op
BenchmarkFloatSearch10-2     	61142461	        18.78 ns/op
BenchmarkIntSearch2-2        	80376837	        15.03 ns/op
BenchmarkIntSearch10-2       	54109246	        22.17 ns/op
BenchmarkGetEnv-2            	26952231	        46.96 ns/op	       0 B/op	       0 allocs/op
BenchmarkGetEnvMissing-2     	24062250	        50.51 ns/op
BenchmarkSHA256-2            	   62689	     18296 ns/op
BenchmarkSHA384-2            	   70474	     17190 ns/op
BenchmarkSHA512-2            	   69765	     17052 ns/op

Documentation

Index

Constants

View Source
const (
	Aqua              = 1752220
	Black             = 2303786
	Blue              = 3447003
	DarkAqua          = 1146986
	DarkBlue          = 2123412
	DarkButNotBlack   = 2895667
	DarkGold          = 12745742
	DarkGreen         = 2067276
	DarkGrey          = 9936031
	DarkNavy          = 2899536
	DarkOrange        = 11027200
	DarkPurple        = 7419530
	DarkRed           = 10038562
	DarkVividPink     = 11342935
	DarkerGrey        = 8359053
	Fuschia           = 15418782
	Gold              = 15844367
	Green             = 5763719
	Grey              = 9807270
	LightGrey         = 12370112
	LuminousVividPink = 15277667
	Navy              = 3426654
	NotQuiteBlack     = 2303786
	Orange            = 15105570
	Purple            = 10181046
	Red               = 15548997
	White             = 16777215
	Yellow            = 16705372
)

Integer codes for common colors

View Source
const JSONApplicationType = "application/json; charset=utf-8"

JSONApplicationType is MIME type for json data

Variables

This section is empty.

Functions

func AllowedMethod

func AllowedMethod(handler http.HandlerFunc, methods string) http.HandlerFunc

AllowedMethod is a decorator to get methods

func ContentResponse

func ContentResponse(w http.ResponseWriter, contentType string, response []byte)

ContentResponse writes a http response with a given content type

func DoJSONRequest

func DoJSONRequest(method, url string, requestBody, responseBody interface{}) (*http.Response, error)

DoJSONRequest sends a client JSON request. The responseBody should be a pointer to the address of a struct. If a blank string is passed then it will default to a POST request. Example:

var response exampleStruct

resp, err := DoJSONRequest("POST", "http://example.com", nil, &response)

or

response := new(exampleStruct)

resp, err := DoJSONRequest("POST", "http://example.com", nil, response)

func DownloadFile

func DownloadFile(url, filename string) (ok bool, err error)

DownloadFile downloads a file and writes it to the file path. Overwrites any file at the path

func FloatSearch

func FloatSearch(target float64, array []float64) bool

FloatSearch checks an array of float64 to see if the target float is in it

func GenerateRandInt

func GenerateRandInt(x int) (returnValue int, returnError error)

GenerateRandInt securely generate a random int64. The input is the maximun value that the random int can be

func GetEnv

func GetEnv(key, fallback string) string

GetEnv checks if the key exists in the environment variables. If yes then returns that value and if not returns default value

func GetEnvSecret

func GetEnvSecret(secretName string) (secret string)

GetEnvSecret will get either a OS environment variable. If there is no environment variable set it will check to see if a variable with _FILE is set. If so then it it will read the secret name as a filepath and return the content

func HashFile

func HashFile(algorithm string, filepath string) (value string, err error)

HashFile generates a string hash of the given file path. Supported hashing algorithm: sha256, sha384, and sha512.

func IntSearch

func IntSearch(target int, array []int) bool

IntSearch checks an array of ints to see if the target int is in it

func JSONMarshalResponse

func JSONMarshalResponse(w http.ResponseWriter, body interface{})

JSONMarshalResponse writes a http response as JSON. Takes interface as input

func JSONResponse

func JSONResponse(w http.ResponseWriter, response []byte)

JSONResponse writes a http response as JSON. Taking a byte array as input

func ParseYamlOrJSON

func ParseYamlOrJSON(fileName string, outputInterface interface{}) (err error)

ParseYamlOrJSON will detect if a file is either a JSON or YAML file and marshal it to the provided interface. Yaml files can end in .yml or .yaml

Example:

var response exampleStruct

if err := ParseYamlOrJSON("helloworld.json", &response); err != nil { log.Fatal(err)}

OR

response := new(exampleStruct)

err := ParseYamlOrJSON("helloworld.yml" response); err != nil { log.Fatal(err)}

func SkipRoot

func SkipRoot(jsonBlob []byte) json.RawMessage

SkipRoot skips the root struct of a JSON message. Taken from https://stackoverflow.com/a/20873511

func StringResponse

func StringResponse(w http.ResponseWriter, response string)

StringResponse writes a http response as a string

func StringSearch

func StringSearch(target string, array []string) bool

StringSearch checks an array of strings to see if the target string is in it

Types

This section is empty.

Jump to

Keyboard shortcuts

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