Skip to content

Commit

Permalink
Move the declaration type and identifier parsing code into the shared…
Browse files Browse the repository at this point in the history
… ddm pacakge
  • Loading branch information
jessepeterson committed Aug 21, 2023
1 parent da651c7 commit 3ebc4a0
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 19 deletions.
21 changes: 21 additions & 0 deletions ddm/path.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package ddm

import (
"errors"
"fmt"
"strings"
)

// ParseDeclarationPath parses path to separate out the declaration type and identifier.
// Typically this will be a "a/b" path where "a" is the type and and "b"
// is the declaration.
func ParseDeclarationPath(path string) (string, string, error) {
split := strings.SplitN(path, "/", 2)
if len(split) != 2 {
return "", "", fmt.Errorf("invalid path element count: %d", len(split))
}
if split[0] == "" || split[1] == "" {
return "", "", errors.New("empty type or identifier path elements")
}
return split[0], split[1], nil
}
6 changes: 2 additions & 4 deletions http/ddm/ddm_test.go → ddm/path_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package ddm

import (
"testing"
)
import "testing"

func TestPathSplit(t *testing.T) {
for _, v := range []struct {
Expand Down Expand Up @@ -31,7 +29,7 @@ func TestPathSplit(t *testing.T) {
},
} {
t.Run("parse-"+v.path, func(t *testing.T) {
rType, rDecl, err := parseDeclarationPath(v.path)
rType, rDecl, err := ParseDeclarationPath(v.path)
if err != nil && !v.expectedErr {
t.Errorf("expected no error, but go one: %v", err)
}
Expand Down
16 changes: 1 addition & 15 deletions http/ddm/ddm.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ package ddm
import (
"context"
"errors"
"fmt"
"io"
"net/http"
"strings"

"github.com/jessepeterson/kmfddm/ddm"
httpddm "github.com/jessepeterson/kmfddm/http"
Expand All @@ -26,18 +24,6 @@ const (

var ErrEmptyEnrollmentID = errors.New("empty enrollment ID")

// parseDeclarationPath parses path to separate out the declaration type and identifier.
func parseDeclarationPath(path string) (string, string, error) {
split := strings.SplitN(path, "/", 2)
if len(split) != 2 {
return "", "", fmt.Errorf("invalid path element count: %d", len(split))
}
if split[0] == "" || split[1] == "" {
return "", "", errors.New("empty type or identifier path elements")
}
return split[0], split[1], nil
}

func ErrorAndLog(w http.ResponseWriter, status int, logger log.Logger, msg string, err error) {
logger.Info(logkeys.Message, msg, logkeys.Error, err)
http.Error(w, http.StatusText(status), status)
Expand Down Expand Up @@ -72,7 +58,7 @@ func DeclarationHandler(store storage.DeclarationRetriever, hLogger log.Logger)
ErrorAndLog(w, http.StatusBadRequest, logger, "getting enrollment id", err)
return
}
declarationType, declarationID, err := parseDeclarationPath(r.URL.Path)
declarationType, declarationID, err := ddm.ParseDeclarationPath(r.URL.Path)
if err != nil {
ErrorAndLog(w, http.StatusBadRequest, logger, "parsing path", err)
return
Expand Down

0 comments on commit 3ebc4a0

Please sign in to comment.