https://github.com/tmc/go2oapi
Go to OpenAPI converter
https://github.com/tmc/go2oapi
Last synced: about 1 year ago
JSON representation
Go to OpenAPI converter
- Host: GitHub
- URL: https://github.com/tmc/go2oapi
- Owner: tmc
- License: mit
- Created: 2023-11-07T07:06:17.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2023-11-07T18:45:14.000Z (over 2 years ago)
- Last Synced: 2025-03-24T09:47:11.985Z (over 1 year ago)
- Language: Go
- Size: 12.7 KB
- Stars: 6
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# go2oapi - Go to OpenAPI Converter
go2oapi is a tool for converting Go function declarations into OpenAPI (Swagger) definitions.
It parses your Go source files, identifies function parameters, and generates associated OpenAPI
definitions suitable for use in [OpenAI Function
Calling](https://platform.openai.com/docs/guides/function-calling).
## Features
- **Automated Parsing**: Automatically parses Go source files to find function declarations.
- **Rich OpenAPI Definitions**: Creates comprehensive OpenAPI definitions including types, descriptions, and enums.
- **Error Handling**: Provides clear error messages for unsupported types and parsing issues.
- **Reflection-Free**: Operates without the need for Go reflection, ensuring type safety and straightforward code.
## Installation
You can directly install the cli version of the tool via `go get` (presuming you have go installed correctly).
```bash
go get github.com/tmc/go2oapi/cmd/go2oapi
```
## Usage
You can use go2oapi either as a package or a command line tool.
### Usage as a library
```go
package main
import (
"github.com/tmc/go2oapi"
"log"
)
func main() {
details, err := go2oapi.ParseFunction("path/to/your/go/program/", "YourFunctionName")
if err != nil {
log.Fatalf("Error parsing function: %s\n", err)
}
// Use `details` for further processing or output
}
```
### go2oapi command line
To generate the OpenAPI tool signature for `strings.Join`:
```
$ go2oapi -src $(go env GOROOT)/src/strings -func Join
{
"name": "Join",
"description": "Join concatenates the elements of its first argument to create a single string. The separator string sep is placed between elements in the resulting string.",
"parameters": {
"type": "object",
"properties": {
"elems": {
"type": "array",
"items": {
"type": "string"
}
},
"sep": {
"type": "string"
}
},
"required": [
"elems",
"sep"
]
}
}
```