From 3ebc4a0476e2e25fcec7a014759951ccab80fe84 Mon Sep 17 00:00:00 2001 From: Jesse Peterson Date: Mon, 21 Aug 2023 13:41:23 -0700 Subject: [PATCH] Move the declaration type and identifier parsing code into the shared ddm pacakge --- ddm/path.go | 21 +++++++++++++++++++++ http/ddm/ddm_test.go => ddm/path_test.go | 6 ++---- http/ddm/ddm.go | 16 +--------------- 3 files changed, 24 insertions(+), 19 deletions(-) create mode 100644 ddm/path.go rename http/ddm/ddm_test.go => ddm/path_test.go (93%) diff --git a/ddm/path.go b/ddm/path.go new file mode 100644 index 0000000..407c022 --- /dev/null +++ b/ddm/path.go @@ -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 +} diff --git a/http/ddm/ddm_test.go b/ddm/path_test.go similarity index 93% rename from http/ddm/ddm_test.go rename to ddm/path_test.go index e5cf007..38e7485 100644 --- a/http/ddm/ddm_test.go +++ b/ddm/path_test.go @@ -1,8 +1,6 @@ package ddm -import ( - "testing" -) +import "testing" func TestPathSplit(t *testing.T) { for _, v := range []struct { @@ -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) } diff --git a/http/ddm/ddm.go b/http/ddm/ddm.go index 89e42e7..3ca2f45 100644 --- a/http/ddm/ddm.go +++ b/http/ddm/ddm.go @@ -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" @@ -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) @@ -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