ent2ogen

package module
v0.0.1 Latest Latest
Warning

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

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

README

ent2ogen

Quite often there's a need to expose some database entities through service api.
If you are using ent as a database abstraction layer, and ogen for an API, then you might need to do type conversion between ent and ogen types, like this:

func (s *Server) GetUser(ctx context.Context, params openapi.GetUserParams) (openapi.User, error) {
	u, err := s.db.Users.Get(params.UserID)
	if err != nil {
		return openapi.User{}, fmt.Errorf("query user: %w", err)
	}

	return openapi.User{
		ID:       u.ID,
		Username: u.Username,
		Age:      u.Age,
		// and so on...
	}
}

Writing such conversion by hand is annoying and error-prone (especially when the types are big with deep nesting).
Also these conversions may become out of sync over time because of database or api schema updates.
Ent2ogen solves this problem by generating mapping functions automatically.

How to use

  1. Create openapi schema
  2. Create ent schema
  3. Create entc.go file (sample)
  4. Create generate.go file (sample)
  5. Use following ent schema annotations:
  • ent2ogen.BindTo("") - generate mapping function to specified openapi schema component.
  • ent2ogen.Bind() - similar to BindTo but uses ent schema name by default.
  1. Run go generate

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Annotation

type Annotation struct {
	BindTo string
}

func Bind

func Bind() Annotation

func BindTo

func BindTo(schema string) Annotation

func (*Annotation) Decode

func (a *Annotation) Decode(annotation interface{}) error

Decode unmarshalls the annotation.

func (Annotation) Name

func (Annotation) Name() string

Name implements schema.Annotation interface.

type Config

type Config struct {
	OgenPackage string
	Mappings    map[*gen.Type]*Mapping
}

func (Config) Name

func (Config) Name() string

type EdgeMapping

type EdgeMapping struct {
	From *gen.Edge
	To   *ir.Field
}

type EnumMapping

type EnumMapping struct {
	From gen.Enum
	To   *ir.EnumVariant
}

type Extension

type Extension struct {
	entc.DefaultExtension
	// contains filtered or unexported fields
}

func NewExtension

func NewExtension(cfg ExtensionConfig) (*Extension, error)

func (*Extension) Annotations

func (ex *Extension) Annotations() []entc.Annotation

Annotations of the extension.

func (*Extension) Hooks

func (ex *Extension) Hooks() []gen.Hook

Hooks of the extension.

func (*Extension) Templates

func (ex *Extension) Templates() []*gen.Template

Templates of the extension.

type ExtensionConfig

type ExtensionConfig struct {
	API         *openapi.API
	Types       map[string]*ir.Type
	OgenPackage string
}

type FieldMapping

type FieldMapping struct {
	From  *gen.Field
	To    *ir.Field
	Enums []EnumMapping // only for enum fields
}

type Mapping

type Mapping struct {
	From          *gen.Type
	To            *ir.Type
	FieldMappings []FieldMapping
	EdgeMappings  []EdgeMapping
	// contains filtered or unexported fields
}

Mapping is used to render templates.

func (*Mapping) Comment

func (m *Mapping) Comment() string

func (*Mapping) EntFields

func (m *Mapping) EntFields() []*gen.Field

EntFields returns ent schema fields.

Jump to

Keyboard shortcuts

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