Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to use custom nullable column type formater #1024

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

pivaldi
Copy link

@pivaldi pivaldi commented Nov 19, 2023

Add the option WithNullableNameStrategy in order to use a custom nullable column type formater.

  • [x ] Do only one thing
  • [x ] Non breaking API changes
  • [x ] Tested

What did this pull request do?

Add the option WithNullableNameStrategy in order to use a custom nullable column type formater.

User Case Description

package main

import (
	"fmt"

	"gorm.io/driver/postgres"
	"gorm.io/gen"
	"gorm.io/gorm"
)

func jsonMapFunc(columnType gorm.ColumnType) (dataType string) {
	return "*nullable.Of[nullable.JSON]"
}

var dataMap = map[string]func(gorm.ColumnType) (dataType string){
	"json":  jsonMapFunc,
	"jsonb": jsonMapFunc,
}

func main() {
	config := gen.Config{
		OutPath:       "./query",
		Mode:          gen.WithDefaultQuery | gen.WithQueryInterface,
		ModelPkgPath:  "model",
		FieldNullable: true,
	}

	config.WithNullableNameStrategy(func(fType string) string {
		return fmt.Sprintf("*nullable.Of[%s]", fType)
	})
	config.WithImportPkgPath("github.com/ovya/nullable")

	g := gen.NewGenerator(config)

	gormdb, _ := gorm.Open(postgres.Open("host=127.0.0.1 port=5432 dbname=db user=user password=pass sslmode=disable connect_timeout=30"))
	g.UseDB(gormdb)
	g.WithDataTypeMap(dataMap)

	g.ApplyBasic(
		g.GenerateAllTable()...,
	)

	g.Execute()
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant