Skip to content

Commit

Permalink
Review changes: MarshalYAML comment, strings quotes
Browse files Browse the repository at this point in the history
  • Loading branch information
hedhyw committed Mar 13, 2022
1 parent 22b0d67 commit bffbab6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
15 changes: 15 additions & 0 deletions protoc-gen-openapiv2/internal/genopenapi/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,22 @@ func (m *RawExample) UnmarshalJSON(data []byte) error {
return (*json.RawMessage)(m).UnmarshalJSON(data)
}

// MarshalYAML implements yaml.Marshaler interface.
//
// It converts RawExample to one of yaml-supported types and returns it.
//
// From yaml.Marshaler docs: The Marshaler interface may be implemented
// by types to customize their behavior when being marshaled into a YAML
// document. The returned value is marshaled in place of the original
// value implementing Marshaler.
func (e RawExample) MarshalYAML() (interface{}, error) {
// From docs, json.Unmarshal will store one of next types to data:
// - bool, for JSON booleans;
// - float64, for JSON numbers;
// - string, for JSON strings;
// - []interface{}, for JSON arrays;
// - map[string]interface{}, for JSON objects;
// - nil for JSON null.
var data interface{}
if err := json.Unmarshal(e, &data); err != nil {
return nil, err
Expand Down
14 changes: 9 additions & 5 deletions protoc-gen-openapiv2/internal/genopenapi/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ func TestRawExample(t *testing.T) {
Exp: `"1"`,
}, {
In: RawExample(`{"hello":"worldr"}`),
Exp: "hello:\n" +
" worldr\n",
Exp: `
hello:
worldr
`,
}}

sr := newSpaceReplacer()
Expand Down Expand Up @@ -77,9 +79,11 @@ func TestOpenapiSchemaObjectProperties(t *testing.T) {
t.Run("yaml", func(t *testing.T) {
t.Parallel()

const exp = "example:\n" +
" test1: 1\n" +
" test2: 2\n"
const exp = `
example:
test1: 1
test2: 2
`

sr := newSpaceReplacer()

Expand Down

0 comments on commit bffbab6

Please sign in to comment.