-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fceb538
commit c1c14b2
Showing
1 changed file
with
40 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package openapi | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"os" | ||
"path/filepath" | ||
|
||
_json "github.com/MarkRosemaker/openapi/internal/json" | ||
"github.com/go-json-experiment/json" | ||
) | ||
|
||
func (d Document) WriteJSON(w io.Writer) error { | ||
return json.MarshalWrite(w, d, _json.Options) | ||
} | ||
|
||
func (d *Document) ToJSON() ([]byte, error) { | ||
return json.Marshal(d, _json.Options) | ||
} | ||
|
||
func (d *Document) WriteToFile(path string) error { | ||
switch filepath.Ext(path) { | ||
case ".json": // ok | ||
default: | ||
return fmt.Errorf("unsupported file extension: %s", filepath.Ext(path)) | ||
} | ||
|
||
// create the underlying directories if they don't exist | ||
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil { | ||
return err | ||
} | ||
|
||
f, err := os.Create(path) | ||
if err != nil { | ||
return err | ||
} | ||
defer f.Close() | ||
|
||
return d.WriteJSON(f) | ||
} |