-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Generice dataset ID format. As providers can have their own ID format for their datasets, we needed to remove some UUID assumptions. This PR does that. * Fix typoes.
- Loading branch information
Showing
9 changed files
with
309 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// Copyright 2024 go-dataspace | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package shared | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/go-dataspace/run-dsp/oid" | ||
"github.com/google/uuid" | ||
) | ||
|
||
// IDtoURN generates the URN for the ID. Right now we only support preformatted URNs, | ||
// UUIDs, and OIDs. | ||
func IDToURN(s string) string { | ||
// If the string starts with urn:, we assume the string is already an URN. | ||
if strings.HasPrefix(strings.ToLower(s), "urn:") { | ||
return s | ||
} | ||
|
||
// Check if we are dealing with a UUID. | ||
if u, err := uuid.Parse(s); err == nil { | ||
return u.URN() | ||
} | ||
|
||
// If not, maybe an OID? | ||
if o, err := oid.Parse(s); err == nil { | ||
return o.URN() | ||
} | ||
|
||
// If still unknown, return an unknown URN. | ||
return fmt.Sprintf("urn:unknown:%s", s) | ||
} | ||
|
||
// URNtoRawID strips the URN part and returns the ID without any metadata. | ||
// This function only works on URNs with 3 parts like the uuid or oid URNs. | ||
// This function returns an error when we can't properly split it. | ||
// TODO: Verify that all providers support URNs so that we can remove this function. | ||
func URNtoRawID(s string) (string, error) { | ||
// If the ID doesn't start with "urn:", we just return it. | ||
if !strings.HasPrefix(strings.ToLower(s), "urn:") { | ||
return s, nil | ||
} | ||
|
||
parts := strings.SplitN(s, ":", 3) | ||
// If we don't get the right amount of parts, we return the full string, despite it most likely | ||
// won't be the result we want. This should be exceedingly rare, and we might want to panic here | ||
// instead to make the problem very obvious. | ||
if len(parts) != 3 { | ||
return "", fmt.Errorf("malformed URN: %s", s) | ||
} | ||
|
||
return parts[2], nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// Copyright 2024 go-dataspace | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
// Package OID contains tools to work with OIDs. | ||
package oid | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
// urnPrefix contains the prefix of an OID formatted as an URN. | ||
const urnPrefix = "urn:oid:" | ||
|
||
// OID represents an OID as a slice of ints. | ||
type OID []int64 | ||
|
||
// validOID is a regex that checks if the string starts, and ends, with a number, and only | ||
// contains numbers and periods in between. | ||
var validOID = regexp.MustCompile(`^\d[\d\.]+\d$`) | ||
|
||
// Parse parses an OID string formatted as '1.2.3.4.5...' and returns an OID instance. It returns | ||
// an error if the OID string can't be parsed. | ||
func Parse(s string) (OID, error) { | ||
if len(s) == 0 { | ||
return OID{}, fmt.Errorf("can't parse empty string") | ||
} | ||
s = strings.ToLower(s) | ||
if strings.HasPrefix(s, urnPrefix) { | ||
s = strings.Replace(s, urnPrefix, "", 1) | ||
} | ||
if !validOID.MatchString(s) { | ||
return OID{}, fmt.Errorf("invalid OID: %s", s) | ||
} | ||
parts := strings.Split(s, ".") | ||
oid := make(OID, len(parts)) | ||
for i, n := range parts { | ||
var err error | ||
oid[i], err = strconv.ParseInt(n, 10, 64) | ||
if err != nil { | ||
return oid, fmt.Errorf("could not parse OID string: %w", err) | ||
} | ||
} | ||
return oid, nil | ||
} | ||
|
||
// MustParse parses the OID string, but panics on error. | ||
func MustParse(s string) OID { | ||
oid, err := Parse(s) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return oid | ||
} | ||
|
||
// String returns the string representation of the OID. | ||
func (o OID) String() string { | ||
if o == nil { | ||
o = OID{} | ||
} | ||
parts := make([]string, len(o)) | ||
for i, n := range o { | ||
parts[i] = strconv.FormatInt(n, 10) | ||
} | ||
return strings.Join(parts, ".") | ||
} | ||
|
||
// URN returns the URN of the OID. | ||
func (o OID) URN() string { | ||
s := o.String() | ||
return fmt.Sprintf("%s%s", urnPrefix, s) | ||
} |
Oops, something went wrong.