Merge pull request 'omitempty: Be selective' (#119) from omitemptyer into trunk

Reviewed-on: #119
This commit is contained in:
Daniel Ponte 2025-02-22 15:54:35 -05:00
commit e879468203
2 changed files with 102 additions and 72 deletions

View file

@ -28,14 +28,14 @@ VALUES
` `
type AddAlertParams struct { type AddAlertParams struct {
Time pgtype.Timestamptz `json:"time,omitempty"` Time pgtype.Timestamptz `json:"time"`
TGID int `json:"tgid,omitempty"` TGID int `json:"tgid"`
SystemID int `json:"systemId,omitempty"` SystemID int `json:"systemId"`
Weight *float32 `json:"weight,omitempty"` Weight *float32 `json:"weight"`
Score *float32 `json:"score,omitempty"` Score *float32 `json:"score"`
OrigScore *float32 `json:"origScore,omitempty"` OrigScore *float32 `json:"origScore"`
Notified bool `json:"notified,omitempty"` Notified bool `json:"notified"`
Metadata []byte `json:"metadata,omitempty"` Metadata []byte `json:"metadata"`
} }
func (q *Queries) AddAlert(ctx context.Context, arg AddAlertParams) error { func (q *Queries) AddAlert(ctx context.Context, arg AddAlertParams) error {
@ -95,24 +95,24 @@ $18
` `
type AddCallParams struct { type AddCallParams struct {
ID uuid.UUID `json:"id,omitempty"` ID uuid.UUID `json:"id"`
Submitter *int32 `json:"submitter,omitempty"` Submitter *int32 `json:"submitter"`
System int `json:"system,omitempty"` System int `json:"system"`
Talkgroup int `json:"talkgroup,omitempty"` Talkgroup int `json:"talkgroup"`
CallDate pgtype.Timestamptz `json:"callDate,omitempty"` CallDate pgtype.Timestamptz `json:"callDate"`
AudioName *string `json:"audioName,omitempty"` AudioName *string `json:"audioName"`
AudioBlob []byte `json:"audioBlob,omitempty"` AudioBlob []byte `json:"audioBlob"`
AudioType *string `json:"audioType,omitempty"` AudioType *string `json:"audioType"`
AudioUrl *string `json:"audioUrl,omitempty"` AudioUrl *string `json:"audioUrl"`
Duration *int32 `json:"duration,omitempty"` Duration *int32 `json:"duration"`
Frequency int `json:"frequency,omitempty"` Frequency int `json:"frequency"`
Frequencies []int `json:"frequencies,omitempty"` Frequencies []int `json:"frequencies"`
Patches []int `json:"patches,omitempty"` Patches []int `json:"patches"`
TalkerAlias *string `json:"talkerAlias,omitempty"` TalkerAlias *string `json:"talkerAlias,omitempty"`
TGLabel *string `json:"tgLabel,omitempty"` TGLabel *string `json:"tgLabel"`
TGAlphaTag *string `json:"tgAlphaTag,omitempty"` TGAlphaTag *string `json:"tgAlphaTag"`
TGGroup *string `json:"tgGroup,omitempty"` TGGroup *string `json:"tgGroup"`
Source int `json:"source,omitempty"` Source int `json:"source"`
} }
func (q *Queries) AddCall(ctx context.Context, arg AddCallParams) error { func (q *Queries) AddCall(ctx context.Context, arg AddCallParams) error {
@ -193,24 +193,24 @@ WHERE id = $1
` `
type GetCallRow struct { type GetCallRow struct {
ID uuid.UUID `json:"id,omitempty"` ID uuid.UUID `json:"id"`
Submitter *int32 `json:"submitter,omitempty"` Submitter *int32 `json:"submitter"`
System int `json:"system,omitempty"` System int `json:"system"`
Talkgroup int `json:"talkgroup,omitempty"` Talkgroup int `json:"talkgroup"`
CallDate pgtype.Timestamptz `json:"callDate,omitempty"` CallDate pgtype.Timestamptz `json:"callDate"`
AudioName *string `json:"audioName,omitempty"` AudioName *string `json:"audioName"`
AudioType *string `json:"audioType,omitempty"` AudioType *string `json:"audioType"`
AudioUrl *string `json:"audioUrl,omitempty"` AudioUrl *string `json:"audioUrl"`
Duration *int32 `json:"duration,omitempty"` Duration *int32 `json:"duration"`
Frequency int `json:"frequency,omitempty"` Frequency int `json:"frequency"`
Frequencies []int `json:"frequencies,omitempty"` Frequencies []int `json:"frequencies"`
Patches []int `json:"patches,omitempty"` Patches []int `json:"patches"`
TalkerAlias *string `json:"talkerAlias,omitempty"` TalkerAlias *string `json:"talkerAlias,omitempty"`
TGLabel *string `json:"tgLabel,omitempty"` TGLabel *string `json:"tgLabel"`
TGAlphaTag *string `json:"tgAlphaTag,omitempty"` TGAlphaTag *string `json:"tgAlphaTag"`
TGGroup *string `json:"tgGroup,omitempty"` TGGroup *string `json:"tgGroup"`
Source int `json:"source,omitempty"` Source int `json:"source"`
Transcript *string `json:"transcript,omitempty"` Transcript *string `json:"transcript"`
} }
func (q *Queries) GetCall(ctx context.Context, id uuid.UUID) (GetCallRow, error) { func (q *Queries) GetCall(ctx context.Context, id uuid.UUID) (GetCallRow, error) {
@ -258,10 +258,10 @@ WHERE sc.id = $1
` `
type GetCallAudioByIDRow struct { type GetCallAudioByIDRow struct {
CallDate pgtype.Timestamptz `json:"callDate,omitempty"` CallDate pgtype.Timestamptz `json:"callDate"`
AudioName *string `json:"audioName,omitempty"` AudioName *string `json:"audioName"`
AudioType *string `json:"audioType,omitempty"` AudioType *string `json:"audioType"`
AudioBlob []byte `json:"audioBlob,omitempty"` AudioBlob []byte `json:"audioBlob"`
} }
func (q *Queries) GetCallAudioByID(ctx context.Context, id uuid.UUID) (GetCallAudioByIDRow, error) { func (q *Queries) GetCallAudioByID(ctx context.Context, id uuid.UUID) (GetCallAudioByIDRow, error) {
@ -323,12 +323,12 @@ CASE WHEN $4::TEXT[] IS NOT NULL THEN
` `
type ListCallsCountParams struct { type ListCallsCountParams struct {
Start pgtype.Timestamptz `json:"start,omitempty"` Start pgtype.Timestamptz `json:"start"`
End pgtype.Timestamptz `json:"end,omitempty"` End pgtype.Timestamptz `json:"end"`
TagsAny []string `json:"tagsAny,omitempty"` TagsAny []string `json:"tagsAny"`
TagsNot []string `json:"tagsNot,omitempty"` TagsNot []string `json:"tagsNot"`
TGFilter *string `json:"tgFilter,omitempty"` TGFilter *string `json:"tgFilter"`
LongerThan pgtype.Numeric `json:"longerThan,omitempty"` LongerThan pgtype.Numeric `json:"longerThan"`
} }
func (q *Queries) ListCallsCount(ctx context.Context, arg ListCallsCountParams) (int64, error) { func (q *Queries) ListCallsCount(ctx context.Context, arg ListCallsCountParams) (int64, error) {
@ -384,23 +384,23 @@ FETCH NEXT $9 ROWS ONLY
` `
type ListCallsPParams struct { type ListCallsPParams struct {
Start pgtype.Timestamptz `json:"start,omitempty"` Start pgtype.Timestamptz `json:"start"`
End pgtype.Timestamptz `json:"end,omitempty"` End pgtype.Timestamptz `json:"end"`
TagsAny []string `json:"tagsAny,omitempty"` TagsAny []string `json:"tagsAny"`
TagsNot []string `json:"tagsNot,omitempty"` TagsNot []string `json:"tagsNot"`
TGFilter *string `json:"tgFilter,omitempty"` TGFilter *string `json:"tgFilter"`
LongerThan pgtype.Numeric `json:"longerThan,omitempty"` LongerThan pgtype.Numeric `json:"longerThan"`
Direction string `json:"direction,omitempty"` Direction string `json:"direction"`
Offset int32 `json:"offset,omitempty"` Offset int32 `json:"offset"`
PerPage int32 `json:"perPage,omitempty"` PerPage int32 `json:"perPage"`
} }
type ListCallsPRow struct { type ListCallsPRow struct {
ID uuid.UUID `json:"id,omitempty"` ID uuid.UUID `json:"id"`
CallDate pgtype.Timestamptz `json:"callDate,omitempty"` CallDate pgtype.Timestamptz `json:"callDate"`
Duration *int32 `json:"duration,omitempty"` Duration *int32 `json:"duration"`
SystemID int `json:"systemId,omitempty"` SystemID int `json:"systemId"`
TGID int `json:"tgid,omitempty"` TGID int `json:"tgid"`
TalkerAlias *string `json:"talkerAlias,omitempty"` TalkerAlias *string `json:"talkerAlias,omitempty"`
Incidents int64 `json:"incidents,omitempty"` Incidents int64 `json:"incidents,omitempty"`
} }

View file

@ -12,19 +12,46 @@ import (
"strings" "strings"
) )
var filePaths = []string{ type FileMap map[string]FieldDecider
"./pkg/database/models.go",
"./pkg/database/calls.sql.go", var filePaths = FileMap{
"./pkg/database/models.go": AllFields{},
"./pkg/database/calls.sql.go": FieldMap{
"TalkerAlias": true,
"Incidents": true,
},
}
type FieldDecider interface {
Check(fields []*ast.Ident) bool
}
type FieldMap map[string]bool
func (fm FieldMap) Check(f []*ast.Ident) bool {
for _, v := range f {
if v != nil && fm[v.Name] {
return true
}
}
return false
}
type AllFields struct{}
func (AllFields) Check(_ []*ast.Ident) bool {
return true
} }
func main() { func main() {
// Parse the source code // Parse the source code
for _, v := range filePaths { for k, v := range filePaths {
process(v) process(k, v)
} }
} }
func process(filePath string) { func process(filePath string, fd FieldDecider) {
fset := token.NewFileSet() fset := token.NewFileSet()
f, err := parser.ParseFile(fset, filePath, nil, parser.ParseComments) f, err := parser.ParseFile(fset, filePath, nil, parser.ParseComments)
if err != nil { if err != nil {
@ -36,6 +63,9 @@ func process(filePath string) {
switch x := n.(type) { switch x := n.(type) {
case *ast.StructType: case *ast.StructType:
for _, field := range x.Fields.List { for _, field := range x.Fields.List {
if !fd.Check(field.Names) {
continue
}
if field.Tag == nil { if field.Tag == nil {
continue continue
} }