Skip to content

Commit

Permalink
Merge pull request #8 from JesseCoretta/v1.0.4
Browse files Browse the repository at this point in the history
v1.0.4
  • Loading branch information
JesseCoretta authored May 13, 2024
2 parents 8f48ed6 + 0a77620 commit ffe7968
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 10 deletions.
7 changes: 5 additions & 2 deletions asn.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,11 @@ func (r ASN1Notation) Index(idx int) (nanf NameAndNumberForm, ok bool) {
/*
NewASN1Notation returns an instance of *[ASN1Notation] alongside an error.
Valid input forms for ASN.1 values are string (e.g.: "{iso(1)}") and string
slices (e.g.: []string{"iso(1)", "identified-organization(3)" ...}).
Valid input forms for ASN.1 values are:
- string (e.g.: "{iso(1) ... }")
- string slices (e.g.: []string{"iso(1)", "identified-organization(3)" ...})
- [NameAndNumberForm] slices ([][NameAndNumberForm]{...})
Note that the following root node abbreviations are supported:
Expand Down
30 changes: 22 additions & 8 deletions oid.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,14 @@ func (r OID) Root() (nanf NameAndNumberForm) {
/*
NewOID creates an instance of [OID] and returns it alongside an error.
The correct raw input syntax is the ASN.1 [NameAndNumberForm] sequence syntax, i.e.:
Valid input forms for ASN.1 values are:
{iso(1) identified-organization(3) dod(6)}
- string (e.g.: "{iso(1) ... }")
- string slices (e.g.: []string{"iso(1)", "identified-organization(3)" ...})
- [NameAndNumberForm] slices ([][NameAndNumberForm]{...})
Not all [NameAndNumberForm] values (arcs) require actual names; they can be numbers alone or in the so-called nameAndNumber syntax (name(Number)). For example:
Not all [NameAndNumberForm] values (arcs) require actual names; they can be
numbers alone or in the so-called nameAndNumber syntax (name(Number)). For example:
{iso(1) identified-organization(3) 6}
Expand All @@ -136,10 +139,21 @@ other than as the respective root node.
[NumberForm] values CANNOT be negative, but are unbounded in their magnitude.
*/
func NewOID(x any) (r *OID, err error) {
// prepare temporary instance
t := new(OID)
r = new(OID)

var nfs []string
switch tv := x.(type) {
case []NameAndNumberForm:
t.nanf = ASN1Notation(tv)
if !t.Valid() {
err = errorf("%T instance did not pass validity checks: %#v", t, t)
break
}
r.nanf = t.nanf
r.parsed = true
return
case string:
nfs = fields(condenseWHSP(trimR(trimL(tv, `{`), `}`)))
case []string:
Expand All @@ -149,11 +163,12 @@ func NewOID(x any) (r *OID, err error) {
return
}

for i := 0; i < len(nfs) && err == nil; i++ {
for i := 0; i < len(nfs); i++ {
var nanf *NameAndNumberForm
if nanf, err = NewNameAndNumberForm(nfs[i]); nanf != nil {
t.nanf = append(t.nanf, *nanf)
if nanf, err = NewNameAndNumberForm(nfs[i]); err != nil {
break
}
t.nanf = append(t.nanf, *nanf)
}

if err == nil {
Expand All @@ -162,9 +177,8 @@ func NewOID(x any) (r *OID, err error) {
return
}

r = new(OID)
r.parsed = true
*r = *t
r.nanf = t.nanf
}

return
Expand Down
14 changes: 14 additions & 0 deletions oid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package objectid

import (
"fmt"
"math/big"
"testing"
)

Expand Down Expand Up @@ -148,4 +149,17 @@ func TestOID_bogus(t *testing.T) {
t.Errorf("%s successfully parsed bogus value; expected an error", t.Name())
return
}

if _, err := NewOID([]NameAndNumberForm{
{identifier: `iso`, primaryIdentifier: NumberForm(*big.NewInt(1)), parsed: true},
{identifier: `identified-organization`, primaryIdentifier: NumberForm(*big.NewInt(3)), parsed: true},
}); err != nil {
t.Errorf("%s error: %v", t.Name(), err)
return
}

if _, err := NewOID([]NameAndNumberForm{}); err == nil {
t.Errorf("%s error: %v", t.Name(), err)
return
}
}

0 comments on commit ffe7968

Please sign in to comment.