Skip to content

Commit

Permalink
adding comment and privatise func
Browse files Browse the repository at this point in the history
  • Loading branch information
sontrinh16 committed Dec 15, 2023
1 parent 28aac5b commit 4e25dd3
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 5 deletions.
2 changes: 1 addition & 1 deletion extendeddatacrossword_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ func createTestEdsWithNMT(t *testing.T, codec Codec, shareSize, namespaceSize in
shares[i] = bytes.Repeat([]byte{byte(shareValue)}, shareSize)
}

treeConstructorFn, err := GetTreeConstructorFn("testing-tree")
treeConstructorFn, err := TreeFn("testing-tree")
require.NoError(t, err)

eds, err := ComputeExtendedDataSquare(shares, codec, treeConstructorFn)
Expand Down
4 changes: 2 additions & 2 deletions extendeddatasquare.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (eds *ExtendedDataSquare) UnmarshalJSON(b []byte) error {
return err
}

treeConstructor, err := GetTreeConstructorFn(aux.Tree)
treeConstructor, err := TreeFn(aux.Tree)
if err != nil {
return err
}
Expand Down Expand Up @@ -77,7 +77,7 @@ func ComputeExtendedDataSquare(
return nil, err
}

treeName := GetTreeNameFromConstructorFn(treeCreatorFn)
treeName := getTreeNameFromConstructorFn(treeCreatorFn)
if treeName == "" {
return nil, errors.New("tree name not found")
}
Expand Down
6 changes: 4 additions & 2 deletions tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type Tree interface {
// The keys of this map should be kebab cased. E.g. "default-tree"
var trees = make(map[string]TreeConstructorFn)

// Must be called in the init function
func RegisterTree(treeName string, treeConstructor TreeConstructorFn) error {
if trees[treeName] != nil {
return fmt.Errorf("%s already registered", treeName)
Expand All @@ -34,15 +35,16 @@ func RegisterTree(treeName string, treeConstructor TreeConstructorFn) error {
return nil
}

func GetTreeConstructorFn(treeName string) (TreeConstructorFn, error) {
// Get tree constructor function by tree name from the global map
func TreeFn(treeName string) (TreeConstructorFn, error) {
if trees[treeName] == nil {
return nil, fmt.Errorf("%s not registered yet", treeName)
}

return trees[treeName], nil
}

func GetTreeNameFromConstructorFn(treeConstructor TreeConstructorFn) string {
func getTreeNameFromConstructorFn(treeConstructor TreeConstructorFn) string {
key := ""
for k, v := range trees {
if reflect.DeepEqual(reflect.ValueOf(v), reflect.ValueOf(treeConstructor)) {
Expand Down

0 comments on commit 4e25dd3

Please sign in to comment.