-
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.
mod id.Extract to return an error if no valid id is found
* add function Factory.QIDFromQIHOT (format)
- Loading branch information
1 parent
0ea6502
commit e848778
Showing
3 changed files
with
47 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,32 @@ | ||
package id | ||
|
||
// Extract parses each of the provided strings as an ID. Empty strings are ignored, parsing errors are returned. The | ||
// first ID that is compatible with the provided target code is returned. If an ID has an embedded ID that matches, it | ||
// is returned. | ||
import ( | ||
"strings" | ||
|
||
"github.com/eluv-io/errors-go" | ||
) | ||
|
||
// Extract parses each of the provided strings as an ID. Empty strings are ignored, parsing errors are returned. | ||
// The first ID that is compatible with the provided target code and is valid is returned. If an ID has an embedded | ||
// valid ID that matches, it is returned. | ||
// If no valid ID is found, an error is returned. | ||
func Extract(target Code, ids ...string) (ID, error) { | ||
for _, candidate := range ids { | ||
if candidate != "" { | ||
cid, err := Parse(candidate) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if cid.Code().IsCompatible(target) { | ||
if cid.Code().IsCompatible(target) && cid.IsValid() { | ||
return cid, nil | ||
} | ||
cid = Decompose(cid).Embedded() | ||
if cid.Code().IsCompatible(target) { | ||
if cid.Code().IsCompatible(target) && cid.IsValid() { | ||
return cid, nil | ||
} | ||
} | ||
} | ||
return nil, nil | ||
return nil, errors.E("Extract", errors.K.Invalid, | ||
"reason", "no valid id found", | ||
"ids", strings.Join(ids, ",")) | ||
} |
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