appc/spec — pull request #65 by sgotti (2014-12-22 13:11:03+00:00)
Title: schema: Fix optional Dependency.ImageID

As in #37 Dependency.ImageID is optional.

Unmarshalling a json without a dependency's ImageID will create an empty Hash struct. On Marshall it will call assertValid that will correctly fail.

This patch makes ImageID a pointer. I made a different fix here: 44314165c516487dd7c126545c289de6a5b1ff92. This is the rationale:

The typical and fast solution is to make ImageID a pointer so on Marshall it won't call assertValid as it's a null object (see ).
The downside is that every Dependency struct's user should verify that ImageID isn't nil before doing any operation. Plus Hash has already an IsEmpty function, so there're can be nil ImageID or an empty ImageID creating some confusion.

There's a need for go json to not Marshal "zero"/empty structs like is already done for builtin types if omitempty is specified: golang/go#4357. Until this is accepted/introduced the solution is to use a pointer/interface{} or a custom Marshal function (this is costly if the number of possible empty fields is big due to the possible combinations).

What do you think about the two possible solutions?

Thanks!