This repository was archived by the owner on Aug 6, 2025. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathmisc.go
More file actions
134 lines (121 loc) · 3.19 KB
/
Copy pathmisc.go
File metadata and controls
134 lines (121 loc) · 3.19 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package newt
import (
"fmt"
"path"
"regexp"
"sort"
"strconv"
"strings"
// Caltech Library Packages
"github.com/caltechlibrary/models"
)
//
// These are misc. functions used in various source files defining Newt
//
// mkName assmebles a name or id from a model, action and possible suffix
func mkName(modelId string, action string, suffix string) string {
return fmt.Sprintf("%s_%s%s", modelId, action, suffix)
}
// inList takes a string and compares it with a list of strings. It
// returns true when a match is found, false otherwise
func inList(target string, list []string) bool {
for _, val := range list {
if val == target {
return true
}
}
return false
}
// getAttributeIds returns a list of attribue keys in a maps[string]interface{} structure
func getAttributeIds(m map[string]string) []string {
ids := []string{}
for k, _ := range m {
if k != "" {
ids = append(ids, k)
}
}
if len(ids) > 0 {
sort.Strings(ids)
}
return ids
}
// Get return the first key and value pair
func getValAndLabel(option map[string]string) (string, string, bool) {
for val, label := range option {
return val, label, true
}
return "", "", false
}
// getValueLabelList takes an array of map[string]string and yours a list of
// strings indicating the value and label
func getValueLabelList(list []map[string]string) []string {
options := []string{}
for _, m := range list {
val, label, ok := getValAndLabel(m)
if ok {
options = append(options, fmt.Sprintf("%s %s", val, label))
}
}
return options
}
func getIdFromList(list []string, id string) (string, bool) {
// NOTE: nRe tests if modelId is a string representation of a positive integer
nRe := regexp.MustCompile(`^[0-9]+$`)
// See if we have been given a model number or a name
if isDigit := nRe.Match([]byte(id)); isDigit {
mNo, err := strconv.Atoi(id)
if err == nil {
// Adjust provided integer for zero based index.
if mNo > 0 {
mNo--
} else {
mNo = 0
}
if mNo < len(list) {
if strings.Contains(list[mNo], " ") {
parts := strings.SplitN(list[mNo], " ", 2)
return parts[0], true
}
return list[mNo], true
}
}
}
if models.IsValidVarname(id) {
return id, true
}
return "", false
}
// getItemNoFromList returns the zero based item number from list of items.
// If an item number is provided then it will be adjusted to zero based and
// validated, if a string is provided it will the position in the list that
// is returned.
func getItemNoFromList(list []string, id string) (int, bool) {
// Handle case where ones based number has been provided as a string.
nRe := regexp.MustCompile(`^[0-9]+$`)
if isDigit := nRe.Match([]byte(id)); isDigit {
itemNo, err := strconv.Atoi(id)
if err == nil {
itemNo--
if itemNo >= 0 && itemNo < len(list) {
return itemNo, true
}
}
return -1, false
}
// Handle case where the string needs to be matched.
for i, val := range list {
if val == id {
return i, true
}
}
return -1, false
}
// fNameToNamespace trunes a filename (e.g. app.yaml) into a namespace (e.g. app)
func fNameToNamespace(s string) string {
bName := path.Base(s)
ext := path.Ext(bName)
if len(ext) > 0 {
return strings.TrimSuffix(bName, ext)
}
return bName
}