Skip to content

fsnode: add JSON/YAML support for File/Directory - #1479

Closed
mvo5 wants to merge 4 commits into
osbuild:mainfrom
mvo5:custom-unmarshalers-2
Closed

mvo5 wants to merge 4 commits into
osbuild:mainfrom
mvo5:custom-unmarshalers-2

Conversation

@mvo5

@mvo5 mvo5 commented Apr 29, 2025 •

Copy link
Copy Markdown
Contributor

More unmarshal support in preparation for the ImageConfig work in #1462 - split to make reviews easier, the fsnode ones are a bit more tricky.


fsnode: validate unknown keys in fsnode.{Dir,File}

When unmarshaling fsnode.{Dir,File}, also validate that no
extra keys are used.


fsnode: add JSON/YAML support for File/Directory

When we move ImageConfig into YAML we will need support for
fsnode.File as it is used as part of the ImageConfig.

This commit adds support for JSON/YAML unmarshaling.


fsnode: use path/filepath instead of path

When validating paths in the fsnode module, use the filepath
module instead of the (more generic) path.

@mvo5
mvo5 force-pushed the custom-unmarshalers-2 branch from 152ac25 to e5403fd Compare April 30, 2025 16:51
@mvo5
mvo5 marked this pull request as ready for review April 30, 2025 16:51
@mvo5
mvo5 requested a review from a team as a code owner April 30, 2025 16:51
@mvo5
mvo5 force-pushed the custom-unmarshalers-2 branch from e5403fd to 963c366 Compare May 5, 2025 08:04
@mvo5
mvo5 requested a review from thozza May 5, 2025 16:18
mvo5 added 4 commits May 6, 2025 13:53
When validating paths in the fsnode module, use the `filepath`
module instead of the (more generic) `path`.
When we move `ImageConfig` into YAML we will need support for
`fsnode.File` as it is used as part of the ImageConfig.

This commit adds support for JSON/YAML unmarshaling.
When unmarshaling fsnode.{Dir,File}, also validate that no
extra keys are used.
In the `type Directory struct` we current use:
```
	ensureParentDirs bool `rename:"ensure_parent_dirs"`
```
We cannot use a "json" tag here because "go vet" complains
that it is used on an unexported field and "go vet" cannot
ignore lines.

Longer term it is probably worthwhile to refactor this
code so that custom unmarshaling is not needed, i.e.
just have an unexpored "type directoryJSON" with
exported fields and a public method only type (like
we have now).
@mvo5
mvo5 force-pushed the custom-unmarshalers-2 branch from c62377c to 32a0e95 Compare May 6, 2025 11:54

@achilleas-k achilleas-k left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor comment/question. Otherwise, LGTM.

Comment on lines +14 to +16
// We cannot use a "json" tag here because "go vet" complains
// that it is used on an unexported field and "go vet" cannot
// ignore lines.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like it might be less trouble to export the field. Shouldn't cause any issues, right?

@lzap lzap left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, this is not nice but it is what it is. I am not too familiar with this code yet.

But the helper function might be improved, it almost looks like you want it to be generic so a good contract and maybe separating it into its own file (with a test) might be a good option?


// XXX: ideally we would also check here that we do not
// get extra/mistyped fields
var m map[string]interface{}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick, any is so much faster to type.

Suggested change
var m map[string]interface{}
var m map[string]any

}

// XXX: ideally we would also check here that we do not
// get extra/mistyped fields

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally, I do not like writing TODOs in the code, I tend too either do stuff or file a ticket. But the codebase is already full of these, so I will just rant about it and that's about it :-)

collect(val, val.Type())

return fieldNames
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function contract should take the JSON map as input and only return error, this is way to open for no reason. The details on how this is done should be completely hidden. Speaking of:

Structs usually have just few fields, there is no reason to use map, arrays or slices are usually faster up to dozens of elements than hashing particularly when they need to be built. I suggest to use just slice and use slices.Contain in an unsorted slice which will be faster than building the hash only to look it up few times.

This function really deserves a separate unit test on its own.

Also, this is ugly and I do not understand how we got here. Is this really the only option? Why the field cannot be just exported?

@mvo5

mvo5 commented May 6, 2025

Copy link
Copy Markdown
Contributor Author

Thanks for the feedback - I agree that the map[string]any unmarshaller is not nice (it was quick and expedient as its really just a distraction from my main goal to get the image config into YAML). But I agree we should not compromise on quality!

I opened #1500 with an alternative approach that use more buld-in go featurs and will close this one (at least for now).

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.

3 participants