/
tools.go
76 lines (61 loc) · 1.65 KB
/
tools.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Tideland GoREST - REST - Tools
//
// Copyright (C) 2009-2017 Frank Mueller / Tideland / Oldenburg / Germany
//
// All rights reserved. Use of this source code is governed
// by the new BSD license.
package rest
//--------------------
// IMPORTS
//--------------------
import (
"fmt"
"net/url"
"strings"
)
//--------------------
// LANGUAGE
//--------------------
// Language is the valued language a request accepts as response.
type Language struct {
Locale string
Value float64
}
// Languages is the ordered set of accepted languages.
type Languages []Language
// Len returns the number of languages to fulfill the sort interface.
func (ls Languages) Len() int {
return len(ls)
}
// Less returns if the language with the index i has a smaller
// value than the one with index j to fulfill the sort interface.
func (ls Languages) Less(i, j int) bool {
return ls[i].Value < ls[j].Value
}
// Swap swaps the languages with the indexes i and j.
func (ls Languages) Swap(i, j int) {
ls[i], ls[j] = ls[j], ls[i]
}
//--------------------
// KEY / VALUE
//--------------------
// KeyValue assigns a value to a key.
type KeyValue struct {
Key string
Value interface{}
}
// String prints the encoded form key=value for URLs.
func (kv KeyValue) String() string {
return fmt.Sprintf("%v=%v", url.QueryEscape(kv.Key), url.QueryEscape(fmt.Sprintf("%v", kv.Value)))
}
// KeyValues is a number of key/value pairs.
type KeyValues []KeyValue
// String prints the encoded form key=value joind by & for URLs.
func (kvs KeyValues) String() string {
kvss := make([]string, len(kvs))
for i, kv := range kvs {
kvss[i] = kv.String()
}
return strings.Join(kvss, "&")
}
// EOF