docstoregen

package module
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2024 License: Apache-2.0 Imports: 14 Imported by: 0

README

Docstore Gen

Friendly & Safer GO CDK Docstore, inspired by go-gorm/gen.

Go Reference Release Go Report Card Coverage Status Build GitHub issues License FOSSA Status

Overview

  • Idiomatic & Reusable API from Dynamic Query
  • 100% Type-safe DAO API without interface{}
  • Struct to Query follows Docstore conventions
  • Designed to be used with the Go CDK Docstore
  • Docstore under the hood, supports all features that Docstore querying supports

Getting Started

Maintainers

@bartventer

Contributing

All contributions are welcome! Open a pull request to request a feature or submit a bug report.

License

Released under the Apache License 2.0.

FOSSA Status

Acknowledgements

This project makes use of code from the go-gorm/gen project. We thank the authors of go-gorm/gen for their work and for making their code available for reuse.

Additionally, this project is built upon the Go CDK Docstore and we appreciate the work of the Go Cloud Development Kit team.

Documentation

Overview

Package docstoregen provides a code generator for the go-cloud.dev/docstore package.

The generator creates a query code file for the go-cloud.dev/docstore package.

The generator's basic configuration is defined in the Config struct.

The generator's main function is Execute, which generates the query code file.

Example:

package main

import (
	"github.com/bartventer/docstore-gen"
)

type User struct {
	ID   string `docstore:"id"`
	Name string `docstore:"name"`
}

func (User) TableName() string { return "user" }

type Copmany struct {
	ID   string `docstore:"id"`
	Name string `docstore:"name"`
}

func (Copmany) TableName() string { return "company" }

func main() {
	g := docstoregen.NewGenerator(docstoregen.Config{
		OutPath: "query",
	})
	g.ApplyInterface(&User{}, &Copmany{})
	g.Execute()
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	OutPath string // OutPath specifies the path where the query code will be generated. Example: "/path/to/project/pkg/query"
	OutFile string // OutFile specifies the name of the query code file. The default value is "gen.go".

	LoggerHandler slog.Handler // LoggerHandler specifies the handler for the logger used by the generator. Example: slog.NewJSONHandler(os.Stdout, nil)
	// contains filtered or unexported fields
}

Config represents the configuration options for generating query code.

func (*Config) Revise

func (cfg *Config) Revise() (err error)

Revise revises the configuration by setting the absolute path for the output directory, generating a default output path if not provided, and setting the output file path. It also sets the query package name based on the output path. Returns an error if the outpath is invalid.

type Generator

type Generator struct {
	Config

	Logger *slog.Logger // logger

	Data map[string]*genInfo //gen query data
}

Generator generate code

func NewGenerator

func NewGenerator(cfg Config) *Generator

NewGenerator creates a new instance of Generator with the given configuration. It revises the configuration and panics if there is an error. If the LoggerHandler is not provided in the configuration, it uses a default JSON handler that writes to os.Stdout. Returns a pointer to the created Generator.

func (*Generator) ApplyInterface

func (g *Generator) ApplyInterface(models ...interface{})

ApplyInterface applies the given models as interfaces to the generator. It converts the models into structs using the generate.ConvertStructs function, checks for any errors, and then applies the generated structs to the generator. If there is an error during the conversion or application process, it logs the error and panics with a "check struct fail" message.

Example:

g := docstoregen.NewGenerator(docstoregen.Config{
	OutPath: "query",
})
g.ApplyInterface(&User{}, &Company{}) // Apply the User and Company models as interfaces to the generator.
g.Execute()

func (*Generator) Execute

func (g *Generator) Execute()

Execute generates code based on the provided specifications. It first generates the query file and then logs the progress. If any error occurs during the generation process, it logs the error and panics. Finally, it logs the completion of code generation.

Example:

g := docstoregen.NewGenerator(docstoregen.Config{
	OutPath: "query",
})
g.ApplyInterface(&User{}, &Company{})
g.Execute() // Generate the query code files.

Directories

Path Synopsis
cmd
Package field provides a way to create field expressions for use with the docstore package.
Package field provides a way to create field expressions for use with the docstore package.
internal

Jump to

Keyboard shortcuts

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