sortprop

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 14, 2021 License: MIT Imports: 1 Imported by: 0

README

sortprop

Package for sortable key-value properties, like HTTP headers, with functions to remove duplicates.

Usage

Key-sortable slices
	…
	kp := sortprop.KeyProperties{
		sortprop.Property{"3", "third"},
		sortprop.Property{"1", "first"},
		sortprop.Property{"2", "second"},
	}
	sort.Sort(kp)
	…
Value-sortable slices
	…
	vp := sortprop.ValueProperties{
		sortprop.Property{"3", "cccc"},
		sortprop.Property{"1", "aaaa"},
		sortprop.Property{"2", "bbbb"},
	}
	sort.Sort(vp)
	…
Appending properties

KeyProperties and ValueProperties are just slices, so use append as usual:

	list := sortprop.KeyValues{}
	p := sortprop.Property{"key", "value"}
	list = append(list, p)
Removing duplicates

These two functions will remove duplicate keys and values:

func UniqueKeys(kp KeyProperties, keeplast bool) KeyProperties
func UniqueValues(vp ValueProperties, keeplast bool) ValueProperties

They return a new slice with only one of each repeated key or value. If keeplast is false, the first of each duplicate element is kept. If keeplast is true, the last one encountered is kept.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type KeyProperties

type KeyProperties []Property

KeyProperties is a slice of Property elements sorted by Key.

func UniqueKeys

func UniqueKeys(kp KeyProperties, keeplast bool) KeyProperties

UniqueKeys returns a slice with only one instance of each unique-key property. If keeplast is true, the last element of the same key will be kept rather than the first. The list will be sorted by Key.

func (KeyProperties) Len

func (p KeyProperties) Len() int

Len is the number of properties.

func (KeyProperties) Less

func (p KeyProperties) Less(a, b int) bool

Less reports whether the property with index a should sort before the property with index b.

func (KeyProperties) Swap

func (p KeyProperties) Swap(a, b int)

Swap the properties with indices a and b.

type Property

type Property struct {
	Key   string
	Value string
}

Property is a key-value pair of strings.

type ValueProperties

type ValueProperties []Property

ValueProperties is a slice of Property elements sorted by Value.

func UniqueValues

func UniqueValues(vp ValueProperties, keeplast bool) ValueProperties

UniqueValues returns a slice with only one instance of each unique value-property. The list will be sorted by Value.

func (ValueProperties) Len

func (p ValueProperties) Len() int

Len is the number of properties.

func (ValueProperties) Less

func (p ValueProperties) Less(a, b int) bool

Less reports whether the property with index a should sort before the property with index b.

func (ValueProperties) Swap

func (p ValueProperties) Swap(a, b int)

Swap the properties with indices a and b.

Jump to

Keyboard shortcuts

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