Temporarily add go-agentx (w/ fixes to lexico ordering)

This commit is contained in:
Pim van Pelt
2025-06-09 17:14:28 +02:00
parent 824496c402
commit 771cc6ff48
45 changed files with 2477 additions and 0 deletions

103
go-agentx/value/oid.go Normal file
View File

@ -0,0 +1,103 @@
// Copyright 2018 The agentx authors
// Licensed under the LGPLv3 with static-linking exception.
// See LICENCE file for details.
package value
import (
"fmt"
"sort"
"strconv"
"strings"
)
// OID defines an OID.
type OID []uint32
// ParseOID parses the provided string and returns a valid oid. If one of the
// subidentifers canot be parsed to an uint32, the function will panic.
func ParseOID(text string) (OID, error) {
var result OID
parts := strings.Split(text, ".")
for _, part := range parts {
subidentifier, err := strconv.ParseUint(part, 10, 32)
if err != nil {
return nil, fmt.Errorf("parse uint [%s]: %w", part, err)
}
result = append(result, uint32(subidentifier))
}
return result, nil
}
// MustParseOID works like ParseOID expect it panics on a parsing error.
func MustParseOID(text string) OID {
result, err := ParseOID(text)
if err != nil {
panic(err)
}
return result
}
// First returns the first n subidentifiers as a new oid.
func (o OID) First(count int) OID {
return o[:count]
}
// CommonPrefix compares the oid with the provided one and
// returns a new oid containing all matching prefix subidentifiers.
func (o OID) CommonPrefix(other OID) OID {
matchCount := 0
for index, subidentifier := range o {
if index >= len(other) || subidentifier != other[index] {
break
}
matchCount++
}
return o[:matchCount]
}
// CompareOIDs returns an integer comparing two OIDs lexicographically.
// The result will be 0 if oid1 == oid2, -1 if oid1 < oid2, +1 if oid1 > oid2.
func CompareOIDs(oid1, oid2 OID) int {
if oid2 != nil {
oid1Length := len(oid1)
oid2Length := len(oid2)
for i := 0; i < oid1Length && i < oid2Length; i++ {
if oid1[i] < oid2[i] {
return -1
}
if oid1[i] > oid2[i] {
return 1
}
}
if oid1Length == oid2Length {
return 0
} else if oid1Length < oid2Length {
return -1
} else {
return 1
}
}
return 1
}
// SortOIDs performs sorting of the OID list.
func SortOIDs(oids []OID) {
sort.Slice(oids, func(i, j int) bool {
return CompareOIDs(oids[i], oids[j]) == -1
})
}
func (o OID) String() string {
var parts []string
for _, subidentifier := range o {
parts = append(parts, fmt.Sprintf("%d", subidentifier))
}
return strings.Join(parts, ".")
}

View File

@ -0,0 +1,70 @@
// Copyright 2018 The agentx authors
// Licensed under the LGPLv3 with static-linking exception.
// See LICENCE file for details.
package value_test
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/posteo/go-agentx/value"
)
func TestCommonPrefix(t *testing.T) {
oid := value.MustParseOID("1.3.6.1.2")
result := oid.CommonPrefix(value.MustParseOID("1.3.6.1.4"))
assert.Equal(t, value.MustParseOID("1.3.6.1"), result)
}
func TestCompareOIDs_Less(t *testing.T) {
oid1 := value.OID{1, 3, 6, 1, 2}
oid2 := value.OID{1, 3, 6, 1, 4}
// oid1 < oid2
expected := -1
assert.Equal(t, expected, value.CompareOIDs(oid1, oid2))
}
func TestCompareOIDs_Greater(t *testing.T) {
oid1 := value.OID{1, 3, 6, 1, 2}
oid2 := value.OID{1, 3, 6, 1, 4}
// oid2 > oid1
expected := 1
assert.Equal(t, expected, value.CompareOIDs(oid2, oid1))
}
func TestCompareOIDs_Equals(t *testing.T) {
oid1 := value.OID{1, 3, 6, 1, 4}
oid2 := value.OID{1, 3, 6, 1, 4}
// oid1 == oid2
expected := 0
assert.Equal(t, expected, value.CompareOIDs(oid1, oid2))
}
func TestCompareOIDs_NilValue(t *testing.T) {
oid1 := value.OID{1, 3, 6, 1, 4}
var oid2 value.OID
// oid2 is nil, thus oid1 is greater
expected := 1
assert.Equal(t, expected, value.CompareOIDs(oid1, oid2))
}
func TestSortOIDs(t *testing.T) {
var oidList []value.OID
oid1 := value.OID{1, 3, 6, 1}
oid2 := value.OID{1, 3, 6, 5, 7}
oid3 := value.OID{1, 3, 6, 1, 12}
oid4 := value.OID{1, 3, 6, 5}
oidList = append(oidList, oid1, oid2, oid3, oid4)
value.SortOIDs(oidList)
var expect []value.OID
expect = append(expect, oid1, oid3, oid4, oid2)
assert.Equal(t, expect, oidList)
}