Skip to content

Commit

Permalink
pathpolicy: unexport PathTrie as it's an implementation detail
Browse files Browse the repository at this point in the history
The `pathpolicy` packages is currently exporting the `PathTrie`
data structure. It seems this is not really used outside of the
package itself and more an implementation detail than a public
API. So just unexport it.
  • Loading branch information
mvo5 authored and ondrejbudai committed Aug 12, 2024
1 parent a1ebe54 commit 5414b17
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 25 deletions.
4 changes: 2 additions & 2 deletions pkg/pathpolicy/path_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ type PathPolicy struct {
}

type PathPolicies struct {
pathTrie *PathTrie[PathPolicy]
pathTrie *pathTrie[PathPolicy]
}

// Create a new PathPolicies trie from a map of path to PathPolicy
func NewPathPolicies(entries map[string]PathPolicy) *PathPolicies {
return &PathPolicies{
pathTrie: NewPathTrieFromMap[PathPolicy](entries),
pathTrie: newPathTrieFromMap[PathPolicy](entries),
}
}

Expand Down
24 changes: 12 additions & 12 deletions pkg/pathpolicy/path_tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,26 @@ import (
"github.com/osbuild/images/internal/common"
)

func TestNewPathTrieFromMap(t *testing.T) {
func TestNewpathTrieFromMap(t *testing.T) {
assert := assert.New(t)

type testCase struct {
entries map[string]*int
trie *PathTrie[*int]
trie *pathTrie[*int]
}

tests := []testCase{
{
entries: map[string]*int{},
trie: &PathTrie[*int]{
trie: &pathTrie[*int]{
Name: []string{},
},
},
{
entries: map[string]*int{
"/": common.ToPtr(1),
},
trie: &PathTrie[*int]{
trie: &pathTrie[*int]{
Name: []string{},
Payload: common.ToPtr(1),
},
Expand All @@ -43,14 +43,14 @@ func TestNewPathTrieFromMap(t *testing.T) {
"/boot": common.ToPtr(7),
"/boot/efi": common.ToPtr(8),
},
trie: &PathTrie[*int]{
trie: &pathTrie[*int]{
Name: []string{},
Payload: common.ToPtr(1),
Paths: []*PathTrie[*int]{
Paths: []*pathTrie[*int]{
{
Name: []string{"boot"},
Payload: common.ToPtr(7),
Paths: []*PathTrie[*int]{
Paths: []*pathTrie[*int]{
{
Name: []string{"efi"},
Payload: common.ToPtr(8),
Expand All @@ -60,11 +60,11 @@ func TestNewPathTrieFromMap(t *testing.T) {
{
Name: []string{"var"},
Payload: common.ToPtr(2),
Paths: []*PathTrie[*int]{
Paths: []*pathTrie[*int]{
{
Name: []string{"lib", "chrony"},
Payload: common.ToPtr(3),
Paths: []*PathTrie[*int]{
Paths: []*pathTrie[*int]{
{
Name: []string{"logs"},
Payload: common.ToPtr(4),
Expand All @@ -74,7 +74,7 @@ func TestNewPathTrieFromMap(t *testing.T) {
{
Name: []string{"lib", "osbuild"},
Payload: common.ToPtr(5),
Paths: []*PathTrie[*int]{
Paths: []*pathTrie[*int]{
{
Name: []string{"store", "cache"},
Payload: common.ToPtr(6),
Expand All @@ -89,7 +89,7 @@ func TestNewPathTrieFromMap(t *testing.T) {
}

for _, tc := range tests {
have := NewPathTrieFromMap(tc.entries)
have := newPathTrieFromMap(tc.entries)
assert.NotNil(have)
assert.Equal(tc.trie, have)
}
Expand All @@ -109,7 +109,7 @@ func TestPathTrieLookup(t *testing.T) {
"/var/lib/chrony/logs": "/var/lib/chrony/logs",
}

trie := NewPathTrieFromMap(entries)
trie := newPathTrieFromMap(entries)

testCases := map[string]string{
"/": "/",
Expand Down
22 changes: 11 additions & 11 deletions pkg/pathpolicy/path_trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ func pathTrieSplitPath(path string) []string {
return strings.Split(path, "/")
}

type PathTrie[T any] struct {
type pathTrie[T any] struct {
Name []string
Paths []*PathTrie[T]
Paths []*pathTrie[T]
Payload T
}

// match checks if the given trie is a prefix of path
func (trie *PathTrie[T]) match(path []string) bool {
func (trie *pathTrie[T]) match(path []string) bool {
if len(trie.Name) > len(path) {
return false
}
Expand All @@ -37,12 +37,12 @@ func (trie *PathTrie[T]) match(path []string) bool {
return true
}

func (trie *PathTrie[T]) get(path []string) (*PathTrie[T], []string) {
func (trie *pathTrie[T]) get(path []string) (*pathTrie[T], []string) {
if len(path) < 1 {
panic("programming error: expected root node")
}

var node *PathTrie[T]
var node *pathTrie[T]
for i := range trie.Paths {
if trie.Paths[i].match(path) {
node = trie.Paths[i]
Expand All @@ -67,11 +67,11 @@ func (trie *PathTrie[T]) get(path []string) (*PathTrie[T], []string) {
return node.get(path[prefix:])
}

func (trie *PathTrie[T]) add(path []string) *PathTrie[T] {
node := &PathTrie[T]{Name: path}
func (trie *pathTrie[T]) add(path []string) *pathTrie[T] {
node := &pathTrie[T]{Name: path}

if trie.Paths == nil {
trie.Paths = make([]*PathTrie[T], 0, 1)
trie.Paths = make([]*pathTrie[T], 0, 1)
}

trie.Paths = append(trie.Paths, node)
Expand All @@ -81,8 +81,8 @@ func (trie *PathTrie[T]) add(path []string) *PathTrie[T] {

// Construct a new trie from a map of paths to their payloads.
// Returns the root node of the trie.
func NewPathTrieFromMap[T any](entries map[string]T) *PathTrie[T] {
root := &PathTrie[T]{Name: []string{}}
func newPathTrieFromMap[T any](entries map[string]T) *pathTrie[T] {
root := &pathTrie[T]{Name: []string{}}

keys := make([]string, 0, len(entries))
for k := range entries {
Expand All @@ -107,7 +107,7 @@ func NewPathTrieFromMap[T any](entries map[string]T) *PathTrie[T] {
// Lookup returns the node that is the prefix of path and
// the unmatched path segment. Must be called on the root
// trie node.
func (root *PathTrie[T]) Lookup(path string) (*PathTrie[T], []string) {
func (root *pathTrie[T]) Lookup(path string) (*pathTrie[T], []string) {

if len(root.Name) != 0 {
panic("programming error: lookup on non-root trie node")
Expand Down

0 comments on commit 5414b17

Please sign in to comment.