Git Product home page Git Product logo

openapi-go-generator's Introduction

openapi-go-generator

openapi gin server api genertor. openapi-go-generator generate gin server and go interface. Auto binding Parameters. Included in path and in query Parameters.
This project is just started. Please feel free to report any bugs and suggestion.

CONFIG

Flat Description Default
-i Input yaml file ./openapi.yaml
-o Output path ./openapigingenertor
-p Package name openapigingenertor

Schema Support Feature

Type Supported
object
string
enum
boolean
int32
int64
float
double
array
required
allOf
oneOf

Sample

git clone https://github.com/chenyunda218/openapi-go-generator
cd openapi-go-generator
go run main.go -o api -p api -i ./openapi.yaml

Installation

Resource openapi yaml

openapi: 3.0.3
info:
  title: Example
  description: |-
    Example
  contact:
    email: [email protected]
  version: 0.0.1
servers:
  - url: http://localhost/api/v1
tags:
  - name: Pet
    description: Api of account
paths:
  /pets:
    post:
      tags:
        - Pet
      operationId: CreateCat
      requestBody:
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/Cat"
      responses:
        "200":
          description: Updated
  /pets/{id}:
    get:
      tags:
        - Pet
      operationId: GetCat
      parameters:
        - name: id
          in: path
          schema:
            type: integer
            format: int64
          required: true
      responses:
        "200":
          description: Updated
components:
  schemas:
    Dog:
      type: object
      properties:
        bark:
          type: boolean
        breed:
          type: string
          enum: [Dingo, Husky, Retriever, Shepherd]
    Cat:
      type: object
      properties:
        hunts:
          type: boolean
        age:
          type: integer

Result

package openapigogenerator

import (
	"strconv"

	"github.com/gin-gonic/gin"
)

type Dog struct {
	Bark  string `json:"bark"`
	Breed string `json:"breed"`
}
type Cat struct {
	Hunts string `json:"hunts"`
	Age   int64  `json:"age"`
}
type PetApiInterface interface {
	CreateCat(gin_context *gin.Context, gin_body Cat)
	GetCat(gin_context *gin.Context, id int64)
}

func CreateCatBinder(api PetApiInterface) func(c *gin.Context) {
	return func(gin_context *gin.Context) {
		var cat Cat
		gin_context.ShouldBindJSON(&cat)
		api.CreateCat(gin_context, cat)
	}
}
func GetCatBinder(api PetApiInterface) func(c *gin.Context) {
	return func(gin_context *gin.Context) {
		id := gin_context.Param("id")
		api.GetCat(gin_context, stringToInt64(id))
	}
}
func PetApiInterfaceMounter(gin_router *gin.Engine, gwg_api_label PetApiInterface) {
	gin_router.POST("/pets", CreateCatBinder(gwg_api_label))
	gin_router.GET("/pets/:id", GetCatBinder(gwg_api_label))
}
func stringToInt32(s string) int32 {
	value, _ := strconv.ParseInt(s, 10, 32)
	return int32(value)
}
func stringToInt64(s string) int64 {
	value, _ := strconv.ParseInt(s, 10, 64)
	return value
}
func stringToFloat32(s string) float32 {
	value, _ := strconv.ParseFloat(s, 32)
	return float32(value)
}
func stringToFloat64(s string) float64 {
	value, _ := strconv.ParseFloat(s, 64)
	return value
}

You should implement PetApiInterface interface.

package main

import (
	"fmt"
	"genen/api"

	"github.com/gin-gonic/gin"
)

type PetApi struct{}

func (PetApi) CreateCat(c *gin.Context, cat api.Cat) {
	fmt.Println(cat)
}

func (PetApi) GetCat(c *gin.Context, id int64) {
	fmt.Println(id)
}

func main() {
	router := gin.Default()
	api.PetApiInterfaceMounter(router, &PetApi{})
	router.Run(":8081")
}

Feature

  • Generate go interface
  • Generate gin router

openapi-go-generator's People

Stargazers

 avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.