From f730c5152e9039fc1d085ced436320b756e3b288 Mon Sep 17 00:00:00 2001 From: Jesse Peterson Date: Fri, 31 May 2024 12:45:42 -0700 Subject: [PATCH] expose mux for status parsing --- ddm/status.go | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/ddm/status.go b/ddm/status.go index 22b97ec..f43054b 100644 --- a/ddm/status.go +++ b/ddm/status.go @@ -199,23 +199,33 @@ func errorHandler(s *StatusReport) jsonpath.HandlerFunc { } } -func newMux(s *StatusReport) *jsonpath.PathMux { - mux := jsonpath.NewPathMux() +// RegisterStatusHandlers attaches default jsonpath Mux handlers for s to mux. +// These default handlers populate the fields of s from a status report. +func RegisterStatusHandlers(mux *jsonpath.PathMux, s *StatusReport) { mux.Handle(pathDeclarations, declarationHandler(s)) mux.Handle(pathManagement, valueHandler(s)) mux.Handle(pathDevice, valueHandler(s)) mux.Handle(pathErrors, errorHandler(s)) - return mux } -// ParseStatus parses the status report from a DDM client. -func ParseStatus(raw []byte) ([]string, *StatusReport, error) { +// ParseStatusUsingMux parses the raw status report from a DDM client using mux. +func ParseStatusUsingMux(raw []byte, mux *jsonpath.PathMux) ([]string, error) { + if mux == nil { + panic("mux is nil") + } v, err := fastjson.ParseBytes(raw) if err != nil { - return nil, nil, fmt.Errorf("parsing json: %w", err) + return nil, fmt.Errorf("parsing json: %w", err) } - s := &StatusReport{Raw: raw} - mux := newMux(s) unhandled, err := mux.JSONPath("", v) + return unhandled, err +} + +// ParseStatus parses the raw status report from a DDM client using mux. +func ParseStatus(raw []byte) ([]string, *StatusReport, error) { + s := &StatusReport{Raw: raw} + mux := jsonpath.NewPathMux() + RegisterStatusHandlers(mux, s) + unhandled, err := ParseStatusUsingMux(s.Raw, mux) return unhandled, s, err }