go2oapi

package module
v0.0.0-...-a374670 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 7, 2023 License: MIT Imports: 8 Imported by: 0

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.

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).

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
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"
    ]
  }
}

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrFunctionNotFound = errors.New("function not found")

Functions

This section is empty.

Types

type DataType

type DataType string

Encodes the type of a field.

const (
	Object  DataType = "object"
	Number  DataType = "number"
	Integer DataType = "integer"
	String  DataType = "string"
	Array   DataType = "array"
	Null    DataType = "null"
	Boolean DataType = "boolean"
)

type Definition

type Definition struct {
	// Type specifies the data type of the schema.
	Type DataType `json:"type,omitempty"`
	// Description is the description of the schema.
	Description string `json:"description,omitempty"`
	// Enum is used to restrict a value to a fixed set of values. It must be an array with at least
	// one element, where each element is unique. You will probably only use this with strings.
	Enum []string `json:"enum,omitempty"`
	// Properties describes the properties of an object, if the schema type is Object.
	Properties map[string]*Definition `json:"properties,omitempty"`
	// Required specifies which properties are required, if the schema type is Object.
	Required []string `json:"required,omitempty"`
	// Items specifies which data type an array contains, if the schema type is Array.
	Items *Definition `json:"items,omitempty"`
}

Definition holds the name and type of a function parameter.

type FunctionDetails

type FunctionDetails struct {
	// Name of the function.
	Name string `json:"name"`
	// Description of the function.
	Description string `json:"description"`
	// Parameters of the function.
	Parameters *Definition `json:"parameters"`
}

FunctionDetails describes a Go function.

func ParseFunction

func ParseFunction(filePath string, funcName string) (*FunctionDetails, error)

ParseFunction parses the Go source code for a specific function.

Example
package main

import (
	"encoding/json"
	"log"
	"os"

	"github.com/tmc/go2oapi"
)

func main() {
	// Assuming we have a file named 'example.go' in the current directory
	// with a function 'HelloWorld' that we want to parse.
	filePath := "testdata/sample-a"
	functionName := "NewWidgetFactory"

	// Parse the function to get the details
	funcDetails, err := go2oapi.ParseFunction(filePath, functionName)
	if err != nil {
		log.Fatalf("Error parsing function: %v\n", err)
	}

	// Output the function details
	// (In a real test, this would be used to validate the output against expected results)
	json.NewEncoder(os.Stdout).Encode(funcDetails)

}
Output:

{"name":"NewWidgetFactory","description":"NewWidgetFactory creates a new widget factory.","parameters":{"type":"object","properties":{"Category":{"type":"string","description":"Category","enum":["foo","bar"]},"FactoryName":{"type":"string","description":"The name of the factory"},"InventoryLevels":{"type":"array","description":"InventoryLevels","items":{"type":"integer"}},"Operational":{"type":"boolean"}},"required":["FactoryName","Category","InventoryLevels","Operational"]}}

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL