diff --git a/README.md b/README.md index 8a4fb987..c4a43db3 100644 --- a/README.md +++ b/README.md @@ -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"` } ``` diff --git a/db.go b/db.go index 30e16ca7..49c939f5 100644 --- a/db.go +++ b/db.go @@ -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) @@ -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)) } @@ -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,