Skip to content

disk: add luks/lvm payload loading support via json/yaml - #1355

Closed
mvo5 wants to merge 1 commit into
osbuild:mainfrom
mvo5:yaml-unmarshal-via-json-2
Closed

mvo5 wants to merge 1 commit into
osbuild:mainfrom
mvo5:yaml-unmarshal-via-json-2

Conversation

@mvo5

@mvo5 mvo5 commented Mar 25, 2025 •

Copy link
Copy Markdown
Contributor

[split out of https://github.com//pull/1339]

This commit adds support to load payloads for LUKS/LVM via
json/yaml. This is needed for PR#1339 where we extract the
partition tables into YAML. We have nested LUKS/LVM partitions
in e.g. the iot simplified installer.

This sadly involves some duplicated code in the Partition,
LUKSContainer, LVMLogicalVolume UnmarshalJSON() code.

Ideally we would write somethign like:

func payloadUnmarshaller[T any](data []byte) (*T, error) {
        type alias T
        var withoutPayload struct {
                alias
                Payload     json.RawMessage `json:"payload"`
                PayloadType string          `json:"payload_type"`
        }
        if err := jsonUnmarshalStrict(data, &withoutPayload); err != nil {
                return nil, fmt.Errorf("cannot unmarshal %q: %w", data, err)
        }
        t := T(withoutPayload.alias)

        payload, err := unmarshalJSONPayload(data)
        t.Payload = payload
        return payload, err
}

func (lv *LVMLogicalVolume) UnmarshalJSON(data []byte) (err error) {
        *lv, err = payloadUnmarshaller[*LVMLogicalVolume](data)
        return err
}

but GO does not let us and sadly its pretty fundamental:

./lvm.go:248:13: cannot use a type parameter as RHS in type declaration

and without type aliases in the custom unmarshaler we run into
an infinite recursion. So unless go supports this we will have to
live with the duplication.

@mvo5
mvo5 requested review from a team and achilleas-k as code owners March 25, 2025 10:48
@mvo5
mvo5 requested review from schuellerf and supakeen March 25, 2025 10:48
@mvo5
mvo5 force-pushed the yaml-unmarshal-via-json-2 branch 2 times, most recently from f6f97e1 to eb6e5d9 Compare March 25, 2025 16:33
@mvo5
mvo5 requested a review from ondrejbudai March 25, 2025 16:34
supakeen
supakeen previously approved these changes Mar 26, 2025

@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.

LGTM. Typo in error message though.

Comment thread pkg/disk/unmarshal.go Resolved
This commit adds support to load payloads for LUKS/LVM via
json/yaml. This is needed for PR#1339 where we extract the
partition tables into YAML. We have nested LUKS/LVM partitions
in e.g. the iot simplified installer.

This sadly involves some duplicated code in the Partition,
LUKSContainer, LVMLogicalVolume `UnmarshalJSON()` code.

Ideally we would write somethign like:
```go
func payloadUnmarshaller[T any](data []byte) (*T, error) {
	type alias T
	var withoutPayload struct {
		alias
		Payload     json.RawMessage `json:"payload"`
		PayloadType string          `json:"payload_type"`
	}
	if err := jsonUnmarshalStrict(data, &withoutPayload); err != nil {
		return nil, fmt.Errorf("cannot unmarshal %q: %w", data, err)
	}
	t := T(withoutPayload.alias)

	payload, err := unmarshalJSONPayload(data)
	t.Payload = payload
	return payload, err
}

func (lv *LVMLogicalVolume) UnmarshalJSON(data []byte) (err error) {
	*lv, err = payloadUnmarshaller[*LVMLogicalVolume](data)
	return err
}
```
but GO does not let us and sadly its pretty fundamental:
```
./lvm.go:248:13: cannot use a type parameter as RHS in type declaration
```
and without type aliases in the custom unmarshaler we run into
an infinite recursion. So unless go supports this we will have to
live with the duplication.
@mvo5
mvo5 requested a review from supakeen March 27, 2025 14:04
@mvo5

mvo5 commented Mar 27, 2025

Copy link
Copy Markdown
Contributor Author

This got merged as part of #1362 so closing it here

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