-
-
Notifications
You must be signed in to change notification settings - Fork 46
/
rolodex_ref_extractor.go
58 lines (50 loc) · 1.04 KB
/
rolodex_ref_extractor.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
// Copyright 2023 Princess B33f Heavy Industries / Dave Shanley
// SPDX-License-Identifier: MIT
package index
import (
"fmt"
"strings"
)
const (
Local RefType = iota
File
HTTP
)
type RefType int
type ExtractedRef struct {
Location string
Type RefType
}
// GetFile returns the file path of the reference.
func (r *ExtractedRef) GetFile() string {
switch r.Type {
case File, HTTP:
location := strings.Split(r.Location, "#/")
return location[0]
default:
return r.Location
}
}
// GetReference returns the reference path of the reference.
func (r *ExtractedRef) GetReference() string {
switch r.Type {
case File, HTTP:
location := strings.Split(r.Location, "#/")
return fmt.Sprintf("#/%s", location[1])
default:
return r.Location
}
}
// ExtractFileType returns the file extension of the reference.
func ExtractFileType(ref string) FileExtension {
if strings.HasSuffix(ref, ".yaml") {
return YAML
}
if strings.HasSuffix(ref, ".yml") {
return YAML
}
if strings.HasSuffix(ref, ".json") {
return JSON
}
return UNSUPPORTED
}