File size: 3,348 Bytes
b110593
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
//                           _       _
// __      _____  __ ___   ___  __ _| |_ ___
// \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \
//  \ V  V /  __/ (_| |\ V /| | (_| | ||  __/
//   \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___|
//
//  Copyright © 2016 - 2024 Weaviate B.V. All rights reserved.
//
//  CONTACT: [email protected]
//

package crossref

import (
	"fmt"
	"net/url"
	"strings"

	"github.com/go-openapi/strfmt"
	"github.com/weaviate/weaviate/entities/models"
)

const (
	_LocalHost = "localhost"
	_Schema    = "weaviate"
)

// Ref is an abstraction of the cross-refs which are specified in a URI format
// in the API. When this type is used it is safe to assume that a Ref is
// semantically valid. This guarantee would not be possible on the URI format,
// as the URI can be well-formed, but not contain the data we expect in it.
// Do not use directly, such as crossref.Ref{}, as you won't have any
// guarantees in this case. Always use one of the parsing options or New()
type Ref struct {
	Local    bool        `json:"local"`
	PeerName string      `json:"peerName"`
	TargetID strfmt.UUID `json:"targetID"`
	Class    string      `json:"className"`
}

// Parse is a safe way to generate a Ref, as it will error if any of the input
// parameters are not as expected.
func Parse(uriString string) (*Ref, error) {
	uri, err := url.Parse(uriString)
	if err != nil || uri.Path == "" {
		return nil, fmt.Errorf("invalid cref URI: %s", err)
	}

	segments := strings.Split(uri.Path, "/")
	class, id, idx := "", "", 1
	switch len(segments) {
	case 3:
		class = segments[1]
		id = segments[2]
		idx = 2
	case 2:
		id = segments[1]
	default:
		return nil, fmt.Errorf(
			"invalid cref URI: path must be of format '<class>/<uuid>', but got '%s'", uri.Path)
	}
	if ok := strfmt.IsUUID(id); !ok {
		return nil, fmt.Errorf("invalid cref URI: %dnd path segment must be uuid, but got '%s'",
			idx, id)
	}

	return &Ref{
		Local:    uri.Host == _LocalHost,
		PeerName: uri.Host,
		TargetID: strfmt.UUID(id),
		Class:    class,
	}, nil
}

// ParseSingleRef is a safe way to generate a Ref from a models.SingleRef, a
// helper construct that represents the API structure. It will error if any of
// the input parameters are not as expected.
func ParseSingleRef(singleRef *models.SingleRef) (*Ref, error) {
	return Parse(string(singleRef.Beacon))
}

// New is a safe way to generate a Reference, as all required arguments must be
// set in the constructor fn
func New(peerName string, class string, target strfmt.UUID) *Ref {
	return &Ref{
		Local:    peerName == _LocalHost,
		PeerName: peerName,
		TargetID: target,
		Class:    class,
	}
}

func NewLocalhost(class string, target strfmt.UUID) *Ref {
	return New(_LocalHost, class, target)
}

func (r *Ref) String() string {
	path := fmt.Sprintf("%s/%s", r.Class, r.TargetID)
	if r.Class == "" {
		path = fmt.Sprintf("/%s", r.TargetID)
	}
	uri := url.URL{
		Host:   r.PeerName,
		Scheme: _Schema,
		Path:   path,
	}

	return uri.String()
}

// SingleRef converts the parsed Ref back into the API helper construct
// containing a stringified representation (URI format) of the Ref
func (r *Ref) SingleRef() *models.SingleRef {
	return &models.SingleRef{
		Beacon: strfmt.URI(r.String()),
	}
}