I cannot unmarshal yaml file what I'm doing wrong ?

7,708 views
Skip to first unread message

Feyyaz Esatoglu

unread,
Feb 15, 2015, 2:07:09 PM2/15/15
to golan...@googlegroups.com
Hi,

I checked out the best practices I think I did same but there is no sign that yaml unmarsahlling the file.
I can see file content after stringing ioutil.ReadFile but the problem occuring while parsing, and there is no error just returns value as empty map.

My Code :


Yaml Package :

Related Blog :

Could you please help what I am doing wrong here ?

Kevin Malachowski

unread,
Feb 15, 2015, 2:13:59 PM2/15/15
to golan...@googlegroups.com
I'm on mobile so I can't type out any code to help, but what happens if you use the Marshal functions in the yaml package to turn a Go struct into yaml?

Also, are you familiar with struct tags?They look like `json:"fieldname"` for the encoding/json package and are optional for that package, but maybe the yaml package has a similar feature and can help out.

Kevin Malachowski

unread,
Feb 15, 2015, 2:19:09 PM2/15/15
to golan...@googlegroups.com
Try making your yaml a pointer, ie &Yaml{}. Right now the call to the UnmarshalYAML method makes a copy, so the "yaml" passed to the fmt.Println is still uninitialized.

Feyyaz Esatoglu

unread,
Feb 15, 2015, 2:23:19 PM2/15/15
to golan...@googlegroups.com
nope I'm trying to import yaml file to structs.
in the link playground that I gave above my structs looks like this.
type (
Parameters struct {
keys map[string]string
}

Yaml struct {
parameters Parameters
}
)

and the yaml which is also above with gists link looks like

parameters:
database_driver: pdo_mysql
database_host: database.local
database_port: 3306
database_name: test

I did try alternatives maps etc. even slices arrays already I know not suitable for string keys but it didin't work

Harmen B

unread,
Feb 15, 2015, 2:27:38 PM2/15/15
to Feyyaz Esatoglu, golang-nuts
On Sun, Feb 15, 2015 at 8:23 PM, Feyyaz Esatoglu <feyya...@gmail.com> wrote:
nope I'm trying to import yaml file to structs.
in the link playground that I gave above my structs looks like this.
type (
Parameters struct {
keys map[string]string
}

Yaml struct {
parameters Parameters
}
)

Those are private fields (they don't start with a capital). Make them Public and try again.

Kevin Malachowski

unread,
Feb 15, 2015, 2:30:53 PM2/15/15
to golan...@googlegroups.com
Make sure to check the error returned from MarshalYAML. Mine says there's an unexpected character, I'm debugging now.

Kevin Malachowski

unread,
Feb 15, 2015, 2:38:07 PM2/15/15
to golan...@googlegroups.com
Here, hope this helps (I got to a computer): https://play.golang.org/p/XN2NeiD1UX

Feyyaz Esatoglu

unread,
Feb 15, 2015, 2:38:34 PM2/15/15
to golan...@googlegroups.com
@Kevin I did commented UnmarshalYaml function to be sure did I initialize the Yaml struct correctly, I think I did ok. But still no sign.

func main() {
var parametersContent *[]byte
var err error
err, parametersContent = readParametersFile(parametersPath)

if err != nil {
panic(err)
}
yaml := Yaml{}
fmt.Println(string(*parametersContent))
yamlParser.Unmarshal(*parametersContent, &yaml)
fmt.Println(yaml)
/*
// UnmarshalYAML not working at the time.
yaml.UnmarshalYAML(parametersContent)
*/

}

@Harmen Capitalized should be Public also it is not important here the privacy valid between packages, not with package itself as I know.

@Kevin how do you debugging ? I think I don't know debugging concept in Go.

Feyyaz Esatoglu

unread,
Feb 15, 2015, 2:46:45 PM2/15/15
to golan...@googlegroups.com
Sorry @Harmen I did misunderstand you, you were right the problem grow out of there.

Thanks @Kevin, I did aware @Harmen's mention from your code, also you did marshalling the yaml, I wasn't need it, but it works like a charm!

Tamás Gulácsi

unread,
Feb 15, 2015, 2:47:08 PM2/15/15
to golan...@googlegroups.com
Capitalize the fields of the destination strict, as reflect needs so.

Craig Mason-Jones

unread,
Feb 15, 2015, 11:06:29 PM2/15/15
to golan...@googlegroups.com
Hi,

There's nothing wrong with your code, except for the capitalization of the fields in Parameters and Yaml structs :-)

The problem is your Parameters struct says:

type (
    Parameters struct {
        Keys map[string]string
    }

    Yaml struct {
        Parameters Parameters
    }
)


The 'real' problem is that your Yaml says:

parameters:
database_driver: pdo_mysql
database_host: database.local
database_port: 3306
database_name: test

Change your Yaml to:

parameters:
   keys:
     database_driver:
pdo_mysql
     database_host: database.local
     database_port: 3306
     database_name: test

The map[string]string is the Keys value of the Parameters object, so you need to reflect this in the Yaml as well, obviously.

All the best,
Craig

Reply all
Reply to author
Forward
0 new messages