diff --git a/internal/tests/ftypes/output.go b/internal/tests/ftypes/output.go index 1679666..5853967 100644 --- a/internal/tests/ftypes/output.go +++ b/internal/tests/ftypes/output.go @@ -6,6 +6,7 @@ import ( "encoding/json" ) +// MarshalJSON marshals as JSON. func (x X) MarshalJSON() ([]byte, error) { type X struct { Int int @@ -19,6 +20,7 @@ func (x X) MarshalJSON() ([]byte, error) { return json.Marshal(&enc) } +// UnmarshalJSON unmarshals from JSON. func (x *X) UnmarshalJSON(input []byte) error { type X struct { Int *int diff --git a/internal/tests/funcoverride/output.go b/internal/tests/funcoverride/output.go index 265911f..6ca5363 100644 --- a/internal/tests/funcoverride/output.go +++ b/internal/tests/funcoverride/output.go @@ -8,6 +8,7 @@ import ( var _ = (*Zo)(nil) +// MarshalJSON marshals as JSON. func (z Z) MarshalJSON() ([]byte, error) { type Z struct { S string `json:"s"` @@ -23,6 +24,7 @@ func (z Z) MarshalJSON() ([]byte, error) { return json.Marshal(&enc) } +// UnmarshalJSON unmarshals from JSON. func (z *Z) UnmarshalJSON(input []byte) error { type Z struct { S *string `json:"s"` @@ -41,6 +43,7 @@ func (z *Z) UnmarshalJSON(input []byte) error { return nil } +// MarshalYAML marshals as YAML. func (z Z) MarshalYAML() (interface{}, error) { type Z struct { S string `json:"s"` @@ -56,6 +59,7 @@ func (z Z) MarshalYAML() (interface{}, error) { return &enc, nil } +// UnmarshalYAML unmarshals from YAML. func (z *Z) UnmarshalYAML(unmarshal func(interface{}) error) error { type Z struct { S *string `json:"s"` @@ -74,6 +78,7 @@ func (z *Z) UnmarshalYAML(unmarshal func(interface{}) error) error { return nil } +// MarshalTOML marshals as TOML. func (z Z) MarshalTOML() (interface{}, error) { type Z struct { S string `json:"s"` @@ -89,6 +94,7 @@ func (z Z) MarshalTOML() (interface{}, error) { return &enc, nil } +// UnmarshalTOML unmarshals from TOML. func (z *Z) UnmarshalTOML(unmarshal func(interface{}) error) error { type Z struct { S *string `json:"s"` diff --git a/internal/tests/mapconv/output.go b/internal/tests/mapconv/output.go index 0d9089c..2a7e657 100644 --- a/internal/tests/mapconv/output.go +++ b/internal/tests/mapconv/output.go @@ -8,6 +8,7 @@ import ( var _ = (*Xo)(nil) +// MarshalJSON marshals as JSON. func (x X) MarshalJSON() ([]byte, error) { type X struct { Map map[replacedString]replacedInt @@ -41,6 +42,7 @@ func (x X) MarshalJSON() ([]byte, error) { return json.Marshal(&enc) } +// UnmarshalJSON unmarshals from JSON. func (x *X) UnmarshalJSON(input []byte) error { type X struct { Map map[replacedString]replacedInt @@ -73,6 +75,7 @@ func (x *X) UnmarshalJSON(input []byte) error { return nil } +// MarshalYAML marshals as YAML. func (x X) MarshalYAML() (interface{}, error) { type X struct { Map map[replacedString]replacedInt @@ -106,6 +109,7 @@ func (x X) MarshalYAML() (interface{}, error) { return &enc, nil } +// UnmarshalYAML unmarshals from YAML. func (x *X) UnmarshalYAML(unmarshal func(interface{}) error) error { type X struct { Map map[replacedString]replacedInt @@ -138,6 +142,7 @@ func (x *X) UnmarshalYAML(unmarshal func(interface{}) error) error { return nil } +// MarshalTOML marshals as TOML. func (x X) MarshalTOML() (interface{}, error) { type X struct { Map map[replacedString]replacedInt @@ -171,6 +176,7 @@ func (x X) MarshalTOML() (interface{}, error) { return &enc, nil } +// UnmarshalTOML unmarshals from TOML. func (x *X) UnmarshalTOML(unmarshal func(interface{}) error) error { type X struct { Map map[replacedString]replacedInt diff --git a/internal/tests/nameclash/output.go b/internal/tests/nameclash/output.go index 4da3bea..db04427 100644 --- a/internal/tests/nameclash/output.go +++ b/internal/tests/nameclash/output.go @@ -11,6 +11,7 @@ import ( var _ = (*yo)(nil) +// MarshalJSON marshals as JSON. func (y Y) MarshalJSON() ([]byte, error) { type Y struct { Foo json0.Foo @@ -28,6 +29,7 @@ func (y Y) MarshalJSON() ([]byte, error) { return json.Marshal(&enc0) } +// UnmarshalJSON unmarshals from JSON. func (y *Y) UnmarshalJSON(input []byte) error { type Y struct { Foo *json0.Foo @@ -58,6 +60,7 @@ func (y *Y) UnmarshalJSON(input []byte) error { return nil } +// MarshalYAML marshals as YAML. func (y Y) MarshalYAML() (interface{}, error) { type Y struct { Foo json0.Foo @@ -75,6 +78,7 @@ func (y Y) MarshalYAML() (interface{}, error) { return &enc0, nil } +// UnmarshalYAML unmarshals from YAML. func (y *Y) UnmarshalYAML(unmarshal func(interface{}) error) error { type Y struct { Foo *json0.Foo @@ -105,6 +109,7 @@ func (y *Y) UnmarshalYAML(unmarshal func(interface{}) error) error { return nil } +// MarshalTOML marshals as TOML. func (y Y) MarshalTOML() (interface{}, error) { type Y struct { Foo json0.Foo @@ -122,6 +127,7 @@ func (y Y) MarshalTOML() (interface{}, error) { return &enc0, nil } +// UnmarshalTOML unmarshals from TOML. func (y *Y) UnmarshalTOML(unmarshal func(interface{}) error) error { type Y struct { Foo *json0.Foo diff --git a/internal/tests/omitempty/output.go b/internal/tests/omitempty/output.go index 45d17dc..e30d984 100644 --- a/internal/tests/omitempty/output.go +++ b/internal/tests/omitempty/output.go @@ -8,6 +8,7 @@ import ( var _ = (*Xo)(nil) +// MarshalJSON marshals as JSON. func (x X) MarshalJSON() ([]byte, error) { type X struct { Int replacedInt `json:",omitempty"` @@ -17,6 +18,7 @@ func (x X) MarshalJSON() ([]byte, error) { return json.Marshal(&enc) } +// UnmarshalJSON unmarshals from JSON. func (x *X) UnmarshalJSON(input []byte) error { type X struct { Int *replacedInt `json:",omitempty"` @@ -31,6 +33,7 @@ func (x *X) UnmarshalJSON(input []byte) error { return nil } +// MarshalYAML marshals as YAML. func (x X) MarshalYAML() (interface{}, error) { type X struct { Int replacedInt `json:",omitempty"` @@ -40,6 +43,7 @@ func (x X) MarshalYAML() (interface{}, error) { return &enc, nil } +// UnmarshalYAML unmarshals from YAML. func (x *X) UnmarshalYAML(unmarshal func(interface{}) error) error { type X struct { Int *replacedInt `json:",omitempty"` @@ -54,6 +58,7 @@ func (x *X) UnmarshalYAML(unmarshal func(interface{}) error) error { return nil } +// MarshalTOML marshals as TOML. func (x X) MarshalTOML() (interface{}, error) { type X struct { Int replacedInt `json:",omitempty"` @@ -63,6 +68,7 @@ func (x X) MarshalTOML() (interface{}, error) { return &enc, nil } +// UnmarshalTOML unmarshals from TOML. func (x *X) UnmarshalTOML(unmarshal func(interface{}) error) error { type X struct { Int *replacedInt `json:",omitempty"` diff --git a/internal/tests/reqfield/output.go b/internal/tests/reqfield/output.go index 11af0a4..33fef31 100644 --- a/internal/tests/reqfield/output.go +++ b/internal/tests/reqfield/output.go @@ -7,6 +7,7 @@ import ( "errors" ) +// MarshalJSON marshals as JSON. func (x X) MarshalJSON() ([]byte, error) { type X struct { Required int `gencodec:"required"` @@ -18,6 +19,7 @@ func (x X) MarshalJSON() ([]byte, error) { return json.Marshal(&enc) } +// UnmarshalJSON unmarshals from JSON. func (x *X) UnmarshalJSON(input []byte) error { type X struct { Required *int `gencodec:"required"` diff --git a/internal/tests/sliceconv/output.go b/internal/tests/sliceconv/output.go index 0f6e719..5e12864 100644 --- a/internal/tests/sliceconv/output.go +++ b/internal/tests/sliceconv/output.go @@ -8,6 +8,7 @@ import ( var _ = (*Xo)(nil) +// MarshalJSON marshals as JSON. func (x X) MarshalJSON() ([]byte, error) { type X struct { Slice []replacedInt @@ -43,6 +44,7 @@ func (x X) MarshalJSON() ([]byte, error) { return json.Marshal(&enc) } +// UnmarshalJSON unmarshals from JSON. func (x *X) UnmarshalJSON(input []byte) error { type X struct { Slice []replacedInt @@ -79,6 +81,7 @@ func (x *X) UnmarshalJSON(input []byte) error { return nil } +// MarshalYAML marshals as YAML. func (x X) MarshalYAML() (interface{}, error) { type X struct { Slice []replacedInt @@ -114,6 +117,7 @@ func (x X) MarshalYAML() (interface{}, error) { return &enc, nil } +// UnmarshalYAML unmarshals from YAML. func (x *X) UnmarshalYAML(unmarshal func(interface{}) error) error { type X struct { Slice []replacedInt @@ -150,6 +154,7 @@ func (x *X) UnmarshalYAML(unmarshal func(interface{}) error) error { return nil } +// MarshalTOML marshals as TOML. func (x X) MarshalTOML() (interface{}, error) { type X struct { Slice []replacedInt @@ -185,6 +190,7 @@ func (x X) MarshalTOML() (interface{}, error) { return &enc, nil } +// UnmarshalTOML unmarshals from TOML. func (x *X) UnmarshalTOML(unmarshal func(interface{}) error) error { type X struct { Slice []replacedInt diff --git a/main.go b/main.go index 2ba2ce0..2dd77cd 100644 --- a/main.go +++ b/main.go @@ -125,6 +125,7 @@ import ( "reflect" "strings" + "github.com/garslo/gogen" "golang.org/x/tools/go/buildutil" "golang.org/x/tools/go/loader" "golang.org/x/tools/imports" @@ -249,22 +250,27 @@ func generate(mtyp *marshalerType, cfg *Config) ([]byte, error) { writeUseOfOverride(w, mtyp.override, mtyp.scope.qualify) } for _, format := range cfg.Formats { + var genMarshal, genUnmarshal gogen.Function switch format { case "json": - writeFunction(w, mtyp.fs, genMarshalJSON(mtyp)) - fmt.Fprintln(w) - writeFunction(w, mtyp.fs, genUnmarshalJSON(mtyp)) + genMarshal = genMarshalJSON(mtyp) + genUnmarshal = genUnmarshalJSON(mtyp) case "yaml": - writeFunction(w, mtyp.fs, genMarshalYAML(mtyp)) - fmt.Fprintln(w) - writeFunction(w, mtyp.fs, genUnmarshalYAML(mtyp)) + genMarshal = genMarshalYAML(mtyp) + genUnmarshal = genUnmarshalYAML(mtyp) case "toml": - writeFunction(w, mtyp.fs, genMarshalTOML(mtyp)) - fmt.Fprintln(w) - writeFunction(w, mtyp.fs, genUnmarshalTOML(mtyp)) + genMarshal = genMarshalTOML(mtyp) + genUnmarshal = genUnmarshalTOML(mtyp) default: return nil, fmt.Errorf("unknown format: %q", format) } + fmt.Fprintf(w, "// %s marshals as %s.", genMarshal.Name, strings.ToUpper(format)) + fmt.Fprintln(w) + writeFunction(w, mtyp.fs, genMarshal) + fmt.Fprintln(w) + fmt.Fprintf(w, "// %s unmarshals from %s.", genUnmarshal.Name, strings.ToUpper(format)) + fmt.Fprintln(w) + writeFunction(w, mtyp.fs, genUnmarshal) fmt.Fprintln(w) } return w.Bytes(), nil