schemagen

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Jun 3, 2024 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package schemagen provides Terraform schema generation.

Example
package main

import (
	"fmt"
	"reflect"

	"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
	"github.com/nitrado/tfconv/schemagen"
)

func main() {
	type TestObject struct {
		Test string `json:"test"`
	}

	docs := func(obj any, sf reflect.StructField) string {
		// Return the documentation for the struct field.
		return ""
	}

	customize := func(obj any, sf *reflect.StructField, typ reflect.Type, s *schema.Schema) (string, reflect.Kind, bool) {
		// Customize the schema output for a sctruct field.
		return "", typ.Kind(), true
	}

	gen := schemagen.New(docs, customize, "json")

	s, err := gen.Struct(TestObject{})
	if err != nil {
		panic(err)
	}

	fmt.Printf("%s", s)

}
Output:

map[test:{
Type: schema.TypeString,
}]

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CustomizerFunc

type CustomizerFunc = func(obj any, sf *reflect.StructField, typ reflect.Type, s *schema.Schema) (string, reflect.Kind, bool)

CustomizerFunc is a function used to customize a schema. It returns an optional function name, the amended kind and a bool determining if the field should be skipped.

type DocsFunc

type DocsFunc = func(obj any, sf reflect.StructField) string

DocsFunc is a function that returns the documentation for a struct field.

type Generator

type Generator struct {
	// contains filtered or unexported fields
}

Generator generates Terraform schemas.

func New

func New(docsFn DocsFunc, customizerFn CustomizerFunc, tag string) *Generator

New returns a schema generator.

func (*Generator) ResolveName

func (g *Generator) ResolveName(sf reflect.StructField) string

ResolveName resolves the name of a field.

func (*Generator) Struct

func (g *Generator) Struct(obj any) (map[string]string, error)

Struct generates the schema from the given object.

Jump to

Keyboard shortcuts

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