This repository has been archived by the owner on Aug 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathidentifier.go
106 lines (92 loc) · 3.19 KB
/
identifier.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
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
// Copyright (C) 2020-2021 Red Hat, Inc.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
package identifier
import (
"encoding/json"
"fmt"
"net/url"
"strings"
"github.com/Masterminds/semver/v3"
)
const (
// semanticVersionKey is the JSON Key representing a SemanticVersion payload.
semanticVersionKey = "version"
// urlKey is the JSON key representing a URL payload.
urlKey = "url"
urlTests = "http://test-network-function.com/tests"
)
// Identifier is a per tnf.Test unique identifier.
type Identifier struct {
// URL stores the unique identifier for a test.
URL string `json:"url" yaml:"url"`
// SemanticVersion stores the version of the test.
SemanticVersion string `json:"version" yaml:"version"`
}
// unmarshalURL deciphers a URL and ensures that the URL parses correctly.
func (i *Identifier) unmarshalURL(objMap map[string]*json.RawMessage) error {
if u, ok := objMap[urlKey]; ok {
var strURL string
if err := json.Unmarshal(*u, &strURL); err != nil {
return err
}
if _, err := url.Parse(strURL); err != nil {
return err
}
i.URL = strURL
return nil
}
return fmt.Errorf("missing required field: \"%s\"", urlKey)
}
// unmarshalSemanticVersion deciphers a SemanticVersion and ensures the SemanticVersion parses correctly.
func (i *Identifier) unmarshalSemanticVersion(objMap map[string]*json.RawMessage) error {
if s, ok := objMap[semanticVersionKey]; ok {
var strSemanticVersion string
if err := json.Unmarshal(*s, &strSemanticVersion); err != nil {
return err
}
if _, err := semver.NewVersion(strSemanticVersion); err != nil {
return err
}
i.SemanticVersion = strSemanticVersion
return nil
}
return fmt.Errorf("missing required field: \"%s\"", semanticVersionKey)
}
// UnmarshalJSON provides a custom JSON Unmarshal function which performs URL and SemanticVersion validation.
func (i *Identifier) UnmarshalJSON(b []byte) error {
var objMap map[string]*json.RawMessage
err := json.Unmarshal(b, &objMap)
if err != nil {
return err
}
if err = i.unmarshalURL(objMap); err != nil {
return err
}
return i.unmarshalSemanticVersion(objMap)
}
// GetShortNameFromIdentifier transform an Identifier into a just test name
// returns empty string if Identifier.URL was not correct about the base domain
// for the url
func GetShortNameFromIdentifier(identifier Identifier) string {
if !strings.HasPrefix(identifier.URL, urlTests+"/") {
return ""
}
itID := strings.ReplaceAll(strings.TrimPrefix(identifier.URL, urlTests+"/"), "/", "-")
return itID
}
func GetIdentifierURLBaseDomain() string {
return urlTests
}