Skip to content

Conversation

@fr33r
Copy link
Collaborator

@fr33r fr33r commented May 17, 2025

Description

These changes add support for ignoring struct fields that have a struct tag with a value of "-", such as morph:"-" or db:"-". This provides an idiomatic way of choosing specific fields that are not desired to be included in the query generation or evaluation process.

Rationale

It is idiomatic in the Golang community to use a struct tag value of "-" to indicate the field should be ignored. This is used for serialization / deserialization in the standard library (such as here in the json package), as well as popular community supported projects such as sqlx.

Suggested Version

v1.5.0

Example Usage

package main

import (
	"fmt"
	"time"

	"github.com/freerware/morph"
)

type Starship struct {
	ID               string
	Name             string
	ManufacturedAt   time.Time
	DecommissionedAt *time.Time
	AccessCode       string `db:"-"`
}

func main() {
	s := Starship{
		ID:               "123",
		Name:             "Millennium Falcon",
		ManufacturedAt:   time.Now(),
		DecommissionedAt: nil,
		AccessCode:       "secret",
	}

	table := morph.Must(morph.Reflect(s, morph.WithTag("db")))
	fmt.Println(table.InsertQuery()) // INSERT INTO starships (decommissioned_at, id, name) VALUES (?, ?, ?);
	fmt.Println(table.UpdateQuery()) // UPDATE starships AS S SET S.decommissioned_at = ?, S.name = ? WHERE 1=1 AND S.id = ?;
	fmt.Println(table.DeleteQuery()) // DELETE FROM starships WHERE 1=1 AND id = ?;
	fmt.Println(table.SelectQuery()) // SELECT decommissioned_at, id, name FROM starships WHERE 1=1 AND id = ?;
	fmt.Println(table.Evaluate(s)) // map[decommissioned_at:<nil> id:123 name:Millennium Falcon]
}

Note

There is a bug above that we are going to address next - do you see it?

@fr33r fr33r self-assigned this May 17, 2025
@fr33r fr33r merged commit e86a7ca into master May 17, 2025
8 checks passed
@fr33r fr33r deleted the freer/ignore-tag branch May 18, 2025 05:42
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.

2 participants