util

package
v0.0.0-...-12d6510 Latest Latest
Warning

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

Go to latest
Published: Jul 11, 2019 License: MIT Imports: 3 Imported by: 1

Documentation

Overview

Package util stores useful helper code.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildFullURL

func BuildFullURL(host string, path string, query map[string]string) (string, error)

BuildFullURL builds a complete URL string in the format host/path?query, where query's keys and values will be formatted as k=v. Returns an util.MalformedURLError if the given URL can't be parsed. This function is a (very simplified version of url.URL.String().

func CleanSlashes

func CleanSlashes(url string) (string, error)

CleanSlashes removes extraneous slashes (like nexus.com///something), which Nexus' API doesn't recognize as valid. Returns an util.MalformedURLError if the given URL can't be parsed.

func MapDiff

func MapDiff(expected map[string]string, actual map[string]string) (diff []string, onlyExpected []string, onlyActual []string)

MapDiff calculates the difference between two maps. It returns three values:

* diff: a slice of strings, holding the keys in both with different values;

* onlyExpected: a slice of keys only in expected;

* onlyActual: a slice of keys only in actual.

When the maps are equal, all three slices will be empty.

Example
package main

import (
	"fmt"

	"sbrubbles.org/go/nexus/util"
)

func main() {
	expected := map[string]string{"a": "a", "b": "b"}
	actual := map[string]string{"a": "aaaa", "c": "c"}

	diff, onlyExpected, onlyActual := util.MapDiff(expected, actual)
	fmt.Println(diff)         // ["a"]
	fmt.Println(onlyExpected) // ["b"]
	fmt.Println(onlyActual)   // ["c"]

	diff, onlyExpected, onlyActual = util.MapDiff(expected, expected)
	fmt.Println(diff)         // []
	fmt.Println(onlyExpected) // []
	fmt.Println(onlyActual)   // []
}
Output:

Types

type ByteSize

type ByteSize float64

ByteSize represents an amount of bytes. float64 is needed since division is required. Implements the fmt.Stringer interface.

const (
	Byte ByteSize = 1 << (10 * iota)
	Kilobyte
	Megabyte
	Gigabyte
)

Some pre-constructed file size units.

func (ByteSize) String

func (size ByteSize) String() string

String implements the fmt.Stringer interface.

type MalformedURLError

type MalformedURLError struct {
	URL string // e.g. http:/:malformed.url.com
}

MalformedURLError is returned when the given URL could not be parsed.

func (MalformedURLError) Error

func (err MalformedURLError) Error() string

Error implements the error interface.

Jump to

Keyboard shortcuts

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