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

add jsonpath and refactor status parsing to use it #39

Merged
merged 3 commits into from
Aug 2, 2023
Merged

Conversation

jessepeterson
Copy link
Owner

We want a way to more flexibly parse our status JSON and hand-off responsibility for portions of the JSON we don't/won't need to support. This is in contrast to supporting the entire declarative management status updates — we'd like a way to delegate just the responsible parties access to the status that they need.

To that end we introduce jsonpath: a Go package for parsing JSON structures and "handling" dot/period-separated paths. If you're familiar with http.ServeMux you'll see the similarities. For example in this JSON:

{
	"foo": {
		"bar": {
			"baz": true,
			"qux": false
		}
	}
}

We can "handle" a path by just specifying ".foo.bar.baz" and your handler will be called once it reaches that place in the JSON structure. For example if you setup your handlers like this:

mux := jsonpath.NewPathMux()
mux.Handle(".foo.bar.baz", myHandler)
unhandled, err := mux.JSONPath("", v)

myHandler would be called back with a fastjson reference and a path string to handle that part of the JSON. You can even nest PathMuxers (though you need to take care about how paths are handled and if you want to handle them in a relative fashion).

In this PR we introduce this the jsonpath package and we refactor the status report parsing in the ddm package to use it. We also log the "unhandled" portions of the JSON to make sure we catch these if we meant to log them. For example:

ts=2023-08-01T13:05:00-07:00 [snip] msg=unhandled status path path=.StatusItems.diskmanagement caller=ddm.go:149
ts=2023-08-01T13:05:00-07:00 [snip] msg=unhandled status path path=.StatusItems.softwareupdate caller=ddm.go:149
ts=2023-08-01T13:05:00-07:00 [snip] msg=unhandled status path path=.StatusItems.services caller=ddm.go:149

@jessepeterson jessepeterson merged commit fb2ddfd into main Aug 2, 2023
3 checks passed
@jessepeterson jessepeterson deleted the jsonpath branch August 2, 2023 18:49
This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant