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

Type conversion between custom type and ARRAY #41

Open
mookjp opened this issue May 19, 2020 · 1 comment
Open

Type conversion between custom type and ARRAY #41

mookjp opened this issue May 19, 2020 · 1 comment
Labels

Comments

@mookjp
Copy link

mookjp commented May 19, 2020

Hi, I'd like to generate code with ARRAY(INT64) with custom types.

If I write DDL and custom types configuration like this:

# sql
CREATE TABLE Items (
  Codes ARRAY<INT64> NOT NULL,
// ...
# custom_column_types.yml
tables:
  - name: Items
    columns:
      Codes: Codes
// or might be
//    Codes: "[]Code"
// type.go

type Codes []Code
type Code int64

const (
  CodeOK Code = 1
  CodeNG Code = 2
)

I want to generate code with conversion between the type (e,g, INT64) and custom type (e.g. Code) if it is an array.
The default template shows it has condition to check Field has CustomType or not so that it generates type conversion code.
It would be helpful to have the method to know the field type is ARRAY or not in this case, but do you have any idea or suggestion for this?

// type.go.tpl

	{{- range .Fields }}
		{{- if .CustomType.IsArray }}
			// type conversion code for array type
		{{- end }}
	{{- end }}
@kazegusuri kazegusuri added the v2 label Dec 26, 2020
@yuriechan
Copy link

yuriechan commented Nov 2, 2023

One of the work around would be to use golang alias type.

Instead of,

// type.go

type Code int64

const (
  CodeOK Code = 1
  CodeNG Code = 2
)

use golang alias

// type.go

type Code = int64

const (
  CodeOK Code = 1
  CodeNG Code = 2
)

then, your custom_column_types.yml would look like,

# custom_column_types.yml
tables:
  - name: Items
    columns:
      Codes: "[]Code"

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

No branches or pull requests

3 participants