All Projects → jbowes → oag

jbowes / oag

Licence: MIT license
Idiomatic Go (Golang) client package generation from OpenAPI documents

Programming Languages

go
31211 projects - #10 most used programming language
Makefile
30231 projects

Projects that are alternatives of or similar to oag

php-json-schema-model-generator
Creates (immutable) PHP model classes from JSON-Schema files including all validation rules as PHP code
Stars: ✭ 36 (-29.41%)
Mutual labels:  code-generator, openapi, code-generation
Dbcc
CAN DBC to C (and CSV, JSON and XML) compiler using the mpc parser combinator library
Stars: ✭ 142 (+178.43%)
Mutual labels:  code-generator, code-generation
Php Code Generator
PHP code generator library
Stars: ✭ 141 (+176.47%)
Mutual labels:  code-generator, code-generation
Jennifer
Jennifer is a code generator for Go
Stars: ✭ 2,257 (+4325.49%)
Mutual labels:  code-generator, code-generation
Geco
Simple code generator based on a console project, running on .Net core and using C# interpolated strings
Stars: ✭ 97 (+90.2%)
Mutual labels:  code-generator, code-generation
Openapi Cli Generator
Generate a CLI from an OpenAPI 3 specification
Stars: ✭ 121 (+137.25%)
Mutual labels:  code-generator, openapi
Swiftcolorgen
A tool that generate code for Swift projects, designed to improve the maintainability of UIColors
Stars: ✭ 152 (+198.04%)
Mutual labels:  code-generator, code-generation
Dogen
Reference implementation of the MASD Code Generator.
Stars: ✭ 44 (-13.73%)
Mutual labels:  code-generator, code-generation
Api Generator
PHP-code generator for Laravel framework, with complete support of JSON-API data format
Stars: ✭ 244 (+378.43%)
Mutual labels:  code-generator, openapi
OpenAPI
A pharo implementation of OpenAPI 3.0.1
Stars: ✭ 20 (-60.78%)
Mutual labels:  openapi, openapi-client
evon
Fast and versatile event dispatcher code generator for Golang
Stars: ✭ 15 (-70.59%)
Mutual labels:  code-generator, code-generation
Goreuse
Generic Code for Go
Stars: ✭ 93 (+82.35%)
Mutual labels:  code-generator, code-generation
Mid
mid is a generic domain-specific language for generating code and documentation
Stars: ✭ 68 (+33.33%)
Mutual labels:  code-generator, code-generation
Toolkit
Collection of useful patterns
Stars: ✭ 137 (+168.63%)
Mutual labels:  code-generator, code-generation
Scala Db Codegen
Scala code/boilerplate generator from a db schema
Stars: ✭ 49 (-3.92%)
Mutual labels:  code-generator, code-generation
Xcassetpacker
A command line tool for converting a folder of images into an .xcasset package for Xcode
Stars: ✭ 150 (+194.12%)
Mutual labels:  code-generator, code-generation
celerio
Celerio is a code generator tool for data-driven application.
Stars: ✭ 73 (+43.14%)
Mutual labels:  code-generator, code-generation
Laravel Code Generator
An intelligent code generator for Laravel framework that will save you time! This awesome tool will help you generate resources like views, controllers, routes, migrations, languages and/or form-requests! It is extremely flexible and customizable to cover many on the use cases. It is shipped with cross-browsers compatible template, along with a client-side validation to modernize your application.
Stars: ✭ 485 (+850.98%)
Mutual labels:  code-generator, code-generation
Colfer
binary serialization format
Stars: ✭ 597 (+1070.59%)
Mutual labels:  code-generator, code-generation
Evolutility Ui Jquery
Model-driven Web UI for CRUD using REST or localStorage.
Stars: ✭ 164 (+221.57%)
Mutual labels:  code-generator, code-generation

oag

Idiomatic Go client package generation from OpenAPI documents

Alpha Quality Build Status GitHub release MIT license codecov Go Report Card


Introduction

Introduction | Examples | Usage | Configuration | Contributing | License

🚧 Disclaimer: oag is alpha quality software. The API of generated code and configuration file format may change without warning between revisions. Please check your generated code before use! 🚧

oag generates idiomatic Go client packages from OpenAPI documents. OpenAPI 2.0 (née Swagger 2.0) is supported, with support for OpenAPI 3.0.0 coming soon.

Features

  • Idiomatic Concise Code: oag generates code modeled after some of Go's best REST API clients, like GitHub and Stripe.
  • Extensible: Generated code is written to a single file. You are free to add your own files to the package, adding behaviour to the generated types.
  • Fast: Code is generated in milliseconds.
  • Commit Friendly: Generated code is deterministic and stable. Multiple runs produce the same output. Document additions and removals create clean diffs.
  • Build Tool Friendly: If the generated code has not changed, nothing is written, and timestamps stay the same. A run of oag won't needlessly trigger other targets in make.
  • Single File Output: All generated code is written to a single file. Removals from an OpenAPI document won't result in orphaned generated files.

Examples

Introduction | Examples | Usage | Configuration | Contributing | License

The minimal petstore OpenAPI 2.0 example (edited for brevity):

// Client is an API client for all endpoints.
type Client struct {
    Pets *PetsClient
    // contains filtered or unexported fields
}

// New returns a new Client with the default configuration.
func New() *Client {}

// Pet is a data type for API communication.
type Pet struct {
    ID   int     `json:"id"`
    Name string  `json:"name"`
    Tag  *string `json:"tag"` // Optional
}

// PetIter Iterates over a result set of Pets.
type PetIter struct {
    // contains filtered or unexported fields
}

// Close closes the PetIter and releases any associated resources. After
// Close, any calls to Current will return an error.
func (i *PetIter) Close() {}

// Current returns the current Pet, and an optional error. Once an error
// has been returned, the PetIter is closed, or the end of iteration is
// reached, subsequent calls to Current will return an error.
func (i *PetIter) Current() (*Pet, error) {}

// Next advances the PetIter and returns a boolean indicating if the end
// has been reached. Next must be called before the first call to Current.
// Calls to Current after Next returns false will return an error.
func (i *PetIter) Next() bool {}

// PetsClient provides access to the /pets APIs
type PetsClient endpoint

// List corresponds to the GET /pets endpoint. Returns all pets from the
// system that the user has access to
func (c *PetsClient) List(ctx context.Context) *PetIter {}

View the complete generated client code

Usage

Introduction | Examples | Usage | Configuration | Contributing | License

Install oag

go get -u github.com/jbowes/oag

Initialize and edit your configuration file

# In the directory under your $GOPATH where your package will live
oag init
# Edit the created .oag.yaml file, following the comments

Generate and commit your code

oag
git add zz_oag_generated.go
git commit -m "Add autogenerated API client"

Implement the error interface for your error types

oag can determine which types are used as errors, but it does not know how you wish to present them.

Create a new file in the same package that implements error for your type. For example, if zz_oag_generated.go contained:

type APIError struct {
	ID	string `json:"id"`
	Message string `json:"message"`
	Class	string `json:"class"`
}

Where ID is some unique value generated per error, and Class is a group of error types, you could put the following in an error.go file:

// Error returns a string representation of this error
func (a *APIError) Error() string {
	return fmt.Sprintf("%s: %s (%s)", a.ID, a.Message, a.Class)
}

Configuration

Introduction | Examples | Usage | Configuration | Contributing | License

oag keeps its configuration in a separate file rather than relying on OpenAPI vendor extensions. If you are not the original author of the OpenAPI document, you don't need to carry patches to it to use oag.

By default, the file .oag.yaml is used for configuration. You may override this with the -c flag.

Configuration Directives

document

Required OpenAPI document to generate a client for.

Example:

document: ./openapi.yaml

package

The package path and optional name to use in the generated code.

Example:

package:
  path: github.com/jbowes/go-sample
  name: sample

types

Optional mapping of definitions to types.

Example:

types:
  SomeDefinedType: github.com/org/package.TypeName

string_formats

An optional map of OpenAPI format values to Go types. Schema fields, array items, and parameters that have these formats will be represented in the generated code with the provided type.

Any types used here must implement the TextMarshaler interface.

Example:

string_formats:
  telephone: github.com/org/package.TelephoneNumber

output

An optional override for the default output file.

Example:

output: zz_oag_generated_client_file.go

boilerplate

A niche configuration directive, allowing you to disable parts of oag's code generation, particularly for shipping generated code for multiple OpenAPI documents in the same package.

Example:

boilerplate:
  base_url: disabled
  backend: disabled
  endpoint: disabled
  client_prefix: PutThisBeforeTypeNames

Contributing

Introduction | Examples | Usage | Configuration | Contributing | License

I would love your help!

oag is still a work in progress. You can help by:

  • Trying oag against different OpenAPI documents, and reporting bugs when the generated code is broken, or suggesting improvements to the generated code.
  • Opening a pull request to resolve an open issue.
  • Adding a feature or enhancement of your own! If it might be big, please open an issue first so we can discuss it.
  • Improving this README or adding other documentation to oag.
  • Letting me know if you're using oag.

License

Introduction | Examples | Usage | Configuration | Contributing | License

MIT

MIT License

Copyright (c) 2017 James Bowes

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].