Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

expose mux for status parsing #81

Merged
merged 1 commit into from
Jun 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions ddm/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}