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

Support for readonly transient field #394 #398

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -261,13 +261,17 @@ type Person struct {
// table.ColMap("Id").Rename("product_id")
// table.ColMap("Price").Rename("unit_price")
// table.ColMap("IgnoreMe").SetTransient(true)
// table.ColMap("IgnoreWrite").Rename("ignore_write").SetTransient(true)
//
// You can optionally declare the field to be a primary key and/or autoincrement
// The readonly fields are transient but available for mapping on reads (e.g. this
// field may be a computed property)
//
type Product struct {
Id int64 `db:"product_id, primarykey, autoincrement"`
Price int64 `db:"unit_price"`
IgnoreMe string `db:"-"`
Id int64 `db:"product_id, primarykey, autoincrement"`
Price int64 `db:"unit_price"`
IgnoreMe string `db:"-"`
IgnoreWrite string `db:"ignore_write, readonly"`
}
```

Expand Down
5 changes: 4 additions & 1 deletion db.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ func (m *DbMap) readStructColumns(t reflect.Type) (cols []*ColumnMap, primaryKey
var isAuto bool
var isPK bool
var isNotNull bool
var isReadOnly bool
for _, argString := range cArguments[1:] {
argString = strings.TrimSpace(argString)
arg := strings.SplitN(argString, ":", 2)
Expand Down Expand Up @@ -349,6 +350,8 @@ func (m *DbMap) readStructColumns(t reflect.Type) (cols []*ColumnMap, primaryKey
isAuto = true
case "notnull":
isNotNull = true
case "readonly":
isReadOnly = true
default:
panic(fmt.Sprintf("Unrecognized tag option for field %v: %v", f.Name, arg))
}
Expand Down Expand Up @@ -390,7 +393,7 @@ func (m *DbMap) readStructColumns(t reflect.Type) (cols []*ColumnMap, primaryKey
cm := &ColumnMap{
ColumnName: columnName,
DefaultValue: defaultValue,
Transient: columnName == "-",
Transient: columnName == "-" || isReadOnly,
fieldName: f.Name,
gotype: gotype,
isPK: isPK,
Expand Down