Skip to content

Commit 7fc8eaf

Browse files
add support struct to unmarshal profiles
1 parent 4ff6d5a commit 7fc8eaf

File tree

1 file changed

+20
-25
lines changed

1 file changed

+20
-25
lines changed

Diff for: arduino/sketch/profiles.go

+20-25
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,18 @@ import (
2929
"gopkg.in/yaml.v3"
3030
)
3131

32+
// projectRaw is a support struct used only to unmarshal the yaml
33+
type projectRaw struct {
34+
ProfilesRaw yaml.Node `yaml:"profiles"`
35+
DefaultProfile string `yaml:"default_profile"`
36+
DefaultFqbn string `yaml:"default_fqbn"`
37+
DefaultPort string `yaml:"default_port,omitempty"`
38+
DefaultProtocol string `yaml:"default_protocol,omitempty"`
39+
}
40+
3241
// Project represents the sketch project file
3342
type Project struct {
34-
ProfilesRaw yaml.Node `yaml:"profiles"`
35-
Profiles []*Profile `yaml:"-"`
43+
Profiles []*Profile `yaml:"profiles"`
3644
DefaultProfile string `yaml:"default_profile"`
3745
DefaultFqbn string `yaml:"default_fqbn"`
3846
DefaultPort string `yaml:"default_port,omitempty"`
@@ -63,7 +71,7 @@ func (p *Project) AsYaml() string {
6371
return res
6472
}
6573

66-
func (p *Project) getProfiles() []*Profile {
74+
func (p *projectRaw) getProfiles() []*Profile {
6775
profiles := []*Profile{}
6876
for i, node := range p.ProfilesRaw.Content {
6977
if node.Tag != "!!str" {
@@ -80,25 +88,7 @@ func (p *Project) getProfiles() []*Profile {
8088
return profiles
8189
}
8290

83-
// Profiles are a list of Profile
84-
type Profiles []*Profile
85-
8691
// UnmarshalYAML decodes a Profiles section from YAML source.
87-
func (p *Profiles) UnmarshalYAML(unmarshal func(interface{}) error) error {
88-
unmarshaledProfiles := map[string]*Profile{}
89-
if err := unmarshal(&unmarshaledProfiles); err != nil {
90-
return err
91-
}
92-
93-
for k, v := range unmarshaledProfiles {
94-
profile := v
95-
profile.Name = k
96-
*p = append(*p, profile)
97-
}
98-
99-
return nil
100-
}
101-
10292
// Profile is a sketch profile, it contains a reference to all the resources
10393
// needed to build and upload a sketch
10494
type Profile struct {
@@ -266,11 +256,16 @@ func LoadProjectFile(file *paths.Path) (*Project, error) {
266256
if err != nil {
267257
return nil, err
268258
}
269-
res := &Project{}
270-
if err := yaml.Unmarshal(data, &res); err != nil {
259+
raw := &projectRaw{}
260+
if err := yaml.Unmarshal(data, &raw); err != nil {
271261
return nil, err
272262
}
273-
res.Profiles = res.getProfiles()
274263

275-
return res, nil
264+
return &Project{
265+
Profiles: raw.getProfiles(),
266+
DefaultProfile: raw.DefaultProfile,
267+
DefaultFqbn: raw.DefaultFqbn,
268+
DefaultPort: raw.DefaultPort,
269+
DefaultProtocol: raw.DefaultProtocol,
270+
}, nil
276271
}

0 commit comments

Comments
 (0)