1- // Copyright (c) Microsoft Corporation.
2- // Licensed under the MIT License.
3-
41package imagecustomizerapi
52
63import (
74 "fmt"
5+
6+ "github.com/microsoft/azurelinux/toolkit/tools/internal/sliceutils"
7+ "gopkg.in/yaml.v3"
88)
99
1010// MountPoint holds the mounting information for each partition.
@@ -17,6 +17,34 @@ type MountPoint struct {
1717 Path string `yaml:"path"`
1818}
1919
20+ // UnmarshalYAML enables MountPoint to handle both a shorthand path and a structured object.
21+ func (p * MountPoint ) UnmarshalYAML (value * yaml.Node ) error {
22+ // Check if the node is a scalar (i.e., single path string).
23+ if value .Kind == yaml .ScalarNode {
24+ // Treat scalar value as the Path directly.
25+ p .Path = value .Value
26+ return nil
27+ }
28+
29+ // yaml.Node.Decode() doesn't respect the KnownFields() option.
30+ // So, manually enforce this.
31+ validFields := []string {"idType" , "options" , "path" }
32+ for i := 0 ; i < len (value .Content ); i += 2 {
33+ key := value .Content [i ].Value
34+ if ! sliceutils .ContainsValue (validFields , key ) {
35+ return fmt .Errorf ("line %d: field %s not found in type %s" , value .Line , key , "MountPoint" )
36+ }
37+ }
38+
39+ // Otherwise, decode as a full MountPoint struct.
40+ type IntermediateTypeMountPoint MountPoint
41+ err := value .Decode ((* IntermediateTypeMountPoint )(p ))
42+ if err != nil {
43+ return fmt .Errorf ("failed to parse MountPoint struct:\n %w" , err )
44+ }
45+ return nil
46+ }
47+
2048// IsValid returns an error if the MountPoint is not valid
2149func (p * MountPoint ) IsValid () error {
2250 err := p .IdType .IsValid ()
0 commit comments