Support typed primary key helpers in gorm and bun writers
This commit is contained in:
@@ -43,26 +43,56 @@ func (m {{.Name}}) SchemaName() string {
|
||||
{{end}}
|
||||
{{if and .Config.GenerateGetID .PrimaryKeyField}}
|
||||
// GetID returns the primary key value
|
||||
func (m {{.Name}}) GetID() int64 {
|
||||
func (m {{.Name}}) GetID() {{.PrimaryKeyIDType}} {
|
||||
{{if .PrimaryKeyIsSQL -}}
|
||||
{{if .PrimaryKeyIsStr -}}
|
||||
return m.{{.PrimaryKeyField}}.String()
|
||||
{{- else -}}
|
||||
return m.{{.PrimaryKeyField}}.Int64()
|
||||
{{- end}}
|
||||
{{- else -}}
|
||||
{{if .PrimaryKeyIsStr -}}
|
||||
return m.{{.PrimaryKeyField}}
|
||||
{{- else -}}
|
||||
return int64(m.{{.PrimaryKeyField}})
|
||||
{{- end}}
|
||||
{{- end}}
|
||||
}
|
||||
{{end}}
|
||||
{{if and .Config.GenerateGetIDStr .PrimaryKeyField}}
|
||||
// GetIDStr returns the primary key as a string
|
||||
func (m {{.Name}}) GetIDStr() string {
|
||||
{{if .PrimaryKeyIsSQL -}}
|
||||
return m.{{.PrimaryKeyField}}.String()
|
||||
{{- else if .PrimaryKeyIsStr -}}
|
||||
return m.{{.PrimaryKeyField}}
|
||||
{{- else -}}
|
||||
return fmt.Sprintf("%d", m.{{.PrimaryKeyField}})
|
||||
{{- end}}
|
||||
}
|
||||
{{end}}
|
||||
{{if and .Config.GenerateSetID .PrimaryKeyField}}
|
||||
// SetID sets the primary key value
|
||||
func (m {{.Name}}) SetID(newid int64) {
|
||||
func (m {{.Name}}) SetID(newid {{.PrimaryKeyIDType}}) {
|
||||
m.UpdateID(newid)
|
||||
}
|
||||
{{end}}
|
||||
{{if and .Config.GenerateUpdateID .PrimaryKeyField}}
|
||||
// UpdateID updates the primary key value
|
||||
func (m *{{.Name}}) UpdateID(newid int64) {
|
||||
func (m *{{.Name}}) UpdateID(newid {{.PrimaryKeyIDType}}) {
|
||||
{{if .PrimaryKeyIsSQL -}}
|
||||
{{if .PrimaryKeyIsStr -}}
|
||||
m.{{.PrimaryKeyField}}.FromString(newid)
|
||||
{{- else -}}
|
||||
m.{{.PrimaryKeyField}}.FromString(fmt.Sprintf("%d", newid))
|
||||
{{- end}}
|
||||
{{- else -}}
|
||||
{{if .PrimaryKeyIsStr -}}
|
||||
m.{{.PrimaryKeyField}} = newid
|
||||
{{- else -}}
|
||||
m.{{.PrimaryKeyField}} = {{.PrimaryKeyType}}(newid)
|
||||
{{- end}}
|
||||
{{- end}}
|
||||
}
|
||||
{{end}}
|
||||
{{if and .Config.GenerateGetIDName .IDColumnName}}
|
||||
|
||||
Reference in New Issue
Block a user