-
Notifications
You must be signed in to change notification settings - Fork 2
/
helper.go
66 lines (60 loc) · 1.66 KB
/
helper.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
//-----------------------------------------------------------------------------
// Copyright (c) 2021-present Detlef Stern
//
// This file is part of Zettelstore.
//
// Zettelstore is licensed under the latest version of the EUPL (European Union
// Public License). Please see file LICENSE.txt for your rights and obligations
// under this license.
//
// SPDX-License-Identifier: EUPL-1.2
// SPDX-FileCopyrightText: 2021-present Detlef Stern
//-----------------------------------------------------------------------------
package box
import (
"net/url"
"strconv"
"time"
"zettelstore.de/z/zettel/id"
)
// GetNewZid calculates a new and unused zettel identifier, based on the current date and time.
func GetNewZid(testZid func(id.Zid) (bool, error)) (id.Zid, error) {
withSeconds := false
for range 90 { // Must be completed within 9 seconds (less than web/server.writeTimeout)
zid := id.New(withSeconds)
found, err := testZid(zid)
if err != nil {
return id.Invalid, err
}
if found {
return zid, nil
}
// TODO: do not wait here unconditionally.
time.Sleep(100 * time.Millisecond)
withSeconds = true
}
return id.Invalid, ErrConflict
}
// GetQueryBool is a helper function to extract bool values from a box URI.
func GetQueryBool(u *url.URL, key string) bool {
_, ok := u.Query()[key]
return ok
}
// GetQueryInt is a helper function to extract int values of a specified range from a box URI.
func GetQueryInt(u *url.URL, key string, min, def, max int) int {
sVal := u.Query().Get(key)
if sVal == "" {
return def
}
iVal, err := strconv.Atoi(sVal)
if err != nil {
return def
}
if iVal < min {
return min
}
if iVal > max {
return max
}
return iVal
}