diff options
| author | mo khan <mo@mokhan.ca> | 2022-08-06 17:08:42 -0600 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2022-08-06 17:08:42 -0600 |
| commit | c45ba93233758e804a3a90997acc2c24141061b4 (patch) | |
| tree | 0b640411e59d8eaa6dc87cefc838f5cacd641e4b | |
| parent | 03bd3bfe2c55446f08009b79b7fc0b708269fe9a (diff) | |
add single path
| -rw-r--r-- | builder/builder.go | 45 | ||||
| -rw-r--r-- | builder/builder_test.go | 24 | ||||
| -rw-r--r-- | builder/document.go | 23 | ||||
| -rw-r--r-- | test/fixtures/users.json | 15 |
4 files changed, 87 insertions, 20 deletions
diff --git a/builder/builder.go b/builder/builder.go index 16f28ed..3f16d71 100644 --- a/builder/builder.go +++ b/builder/builder.go @@ -1,36 +1,53 @@ package builder -type Info struct { - Title string `json:"title"` - Version string `json:"version"` +type Builder interface { + AddPath(string) PathBuilder + Build() *Document } -type Paths struct { +type PathBuilder interface { + Get(string, string) } -type Document struct { - Version string `json:"openapi"` - Info *Info `json:"info"` - Paths *Paths `json:"paths"` +type builder struct { + doc *Document } -type Builder struct { - doc *Document +type pathBuilder struct { + root string + builder *builder } -func New(title, version string) *Builder { - return &Builder{ +func (b *pathBuilder) Get(description string, summary string) { + paths := b.builder.doc.Paths + paths[b.root] = map[string]interface{}{ + "get": map[string]interface{}{ + "description": description, + "summary": summary, + }, + } +} + +func New(title, version string) Builder { + return &builder{ doc: &Document{ Version: "3.1.0", Info: &Info{ Title: title, Version: version, }, - Paths: &Paths{}, + Paths: Paths{}, }, } } -func (b *Builder) Build() *Document { +func (b *builder) AddPath(path string) PathBuilder { + return &pathBuilder{ + root: path, + builder: b, + } +} + +func (b *builder) Build() *Document { return b.doc } diff --git a/builder/builder_test.go b/builder/builder_test.go index 450aca9..6be142d 100644 --- a/builder/builder_test.go +++ b/builder/builder_test.go @@ -1,22 +1,34 @@ package builder import ( - "encoding/json" "testing" "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" "github.com/xlgmokha/openapi/test" ) func TestBuilder(t *testing.T) { t.Run("builds a minimal spec", func(t *testing.T) { + spec := New("The API", "0.1.0").Build() + + assert.Equal(t, + test.Fixture("minimal.json"), + spec.ToJSON(), + ) + }) + + t.Run("adds a path", func(t *testing.T) { builder := New("The API", "0.1.0") - spec := builder.Build() + builder.AddPath("/users").Get( + "List all the users", + "List all the users", + ) - bytes, err := json.MarshalIndent(spec, "", " ") - require.Nil(t, err) + spec := builder.Build() - assert.Equal(t, test.Fixture("minimal.json"), string(bytes)) + assert.Equal(t, + test.Fixture("users.json"), + spec.ToJSON(), + ) }) } diff --git a/builder/document.go b/builder/document.go new file mode 100644 index 0000000..1dbdc7c --- /dev/null +++ b/builder/document.go @@ -0,0 +1,23 @@ +package builder + +import ( + "encoding/json" +) + +type Info struct { + Title string `json:"title"` + Version string `json:"version"` +} + +type Paths map[string]interface{} + +type Document struct { + Version string `json:"openapi"` + Info *Info `json:"info"` + Paths Paths `json:"paths"` +} + +func (document *Document) ToJSON() string { + bytes, _ := json.MarshalIndent(document, "", " ") + return string(bytes) +} diff --git a/test/fixtures/users.json b/test/fixtures/users.json new file mode 100644 index 0000000..12e8de1 --- /dev/null +++ b/test/fixtures/users.json @@ -0,0 +1,15 @@ +{ + "openapi": "3.1.0", + "info": { + "title": "The API", + "version": "0.1.0" + }, + "paths": { + "/users": { + "get": { + "description": "List all the users", + "summary": "List all the users" + } + } + } +} |
