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

How to update by force a zero value? #3901

Closed
sdghchj opened this issue Dec 30, 2020 · 11 comments
Closed

How to update by force a zero value? #3901

sdghchj opened this issue Dec 30, 2020 · 11 comments
Assignees
Labels
type:question general questions

Comments

@sdghchj
Copy link

sdghchj commented Dec 30, 2020

Your Question

I redefine a type and implement interface driver.Value and driver.Scanner,for instance, I want type []string to be stored as a json string, but I need to update it to null even if it is a nil value.

type Example []string

// Value returns m as driver.Value
func (m Example) Value() (driver.Value, error) {
	bin, err := json.Marshal(m)
	return string(bin), err
}

func (m *Example) Scan(v interface{}) error {
	switch data := v.(type) {
	case string:
		return json.Unmarshal([]byte(data), m)
	case []byte:
		return json.Unmarshal(data, m)
	default:
		return fmt.Errorf("invalid value %v for Example to Scan", v)
	}
}

Expected answer

I suggest gorm offer an interface IsZero as a supplement to the method reflect.field.IsZero.
Morever, a field tag nozerononil or something like that is also appreciated.

type Outer struct{
    Inner Example `gorm: "nozero"`
}
@jinzhu
Copy link
Member

jinzhu commented Jan 4, 2021

Update zero fields https://gorm.io/docs/update.html#Update-Selected-Fields

@jinzhu jinzhu closed this as completed Jan 4, 2021
@jinzhu
Copy link
Member

jinzhu commented Jan 4, 2021

I don't think it is necessary to add another API to do the job, sounds like just a update zero-value fields issue, which is same if you are using other data types like: int, string?

@sdghchj
Copy link
Author

sdghchj commented Jan 4, 2021

I don't think it is necessary to add another API to do the job, sounds like just a update zero-value fields issue, which is same if you are using other data types like: int, string?

Selecting explicitly actually does work for zero fields, but if my object has many fields, I don't think it is a good experience everytime to select every feild. Everytime I add or remove a field, I have to add or remove it in the select clause.

@jinzhu
Copy link
Member

jinzhu commented Jan 4, 2021

If you want to select all fields, you can use db.Select("*")

@sdghchj
Copy link
Author

sdghchj commented Jan 4, 2021

If you want to select all fields, you can use db.Select("*")

It is pity that usually there is a few fields that need not to update. More offen I use omit.

@jinzhu
Copy link
Member

jinzhu commented Jan 4, 2021

Select("*").Omit("Name", "Age")

@sdghchj
Copy link
Author

sdghchj commented Jan 4, 2021

Select("*").Omit("Name", "Age")

e, ok

@jasonhp
Copy link

jasonhp commented Jun 16, 2021

Select("*").Omit("Name", "Age")

It doesn't work. Select("*") still ignores all zero values.

Akkadius added a commit to Akkadius/spire that referenced this issue Dec 31, 2021
* Spell editing work

* Fix some re-rendering

* Frame in more fields

* Fix warnings

* Fix re-render preview issues, add skill dropdown

* Add target type select

* Add resist type select

* Add icon selection

* Add tab hover functionality to component

* Spell animation preview component

* Cleanup video toggle changing

* Spell animation

* Animation viewer select mock

* Update SpellEdit.vue

* Implement animation selection

* Add selector pulse animation

* Hover

* Windows fixes (akka)

* Adjust animation selector virtual height

* Add checkboxes

* More checkboxes

* Update SpellEdit.vue

* Update SpellEdit.vue

* Update SpellEdit.vue

* Update SpellEdit.vue

* Form re-arranging

* Update SpellEdit.vue

* Update SpellEdit.vue

* Regenerate models

* Fix operator

* Implement spell create / update (save)

* Hook up spell class selection

* Put deity selector in place

* Preselect icon in icon selector

* Update SpellIconSelector.vue

* Push up pre-selection for spell animation

* Tweaks

* Scroll to pre-selected spell animation

* Implement query free-id-ranges endpoint `query/free-id-ranges/:table/:id`

* Copy pasta

* Pre free id selector work

* Implement free ids reserved

* Move query logic to generic function

* Cleanup

* Highlight fields once changed, change text inputs to update only on change event and not when every single letter is typed

* Handle and display save errors

* Correct all field types

* More field shuffling

* Update SpellEdit.vue

* Update SpellEdit.vue

* Fix item display bugs

* Update eq-expansions.ts

* Add eq fancy buttons

* Beginning of item editor work

* Add in item model preview

* Add item model selection

* Implement Item Icon selection

* Lots of component work around bitmask calculators and plug into item editor

* Fix non-zero update issue and update all controllers and template go-gorm/gorm#3901

* Update item_controller.go

* Mask sure mask is set before rendering component

* Tweak calculators

* Fix items page zero state

* Update Calculators.vue

* More changes

* More component work

* More item editor work

* Add damage tab

* Tweaks

* spell updates

* Update spells.ts

eye of zomm, teleport updates

* Update spells.ts

teleport and succor updates

* Update spells.ts

change size

* Update spells.ts

* Update spells.ts

* Update spells.ts

* Update spells.ts

* Create eq-spell-spa-definitions.ts

* Selector adjustments

* More component tweaks

* Change item edit layout

* Aggregate stats tab

* Update InventorySlotCalculator.vue

* Lower opacity inputs when value is zero for easier scanning

* Update ItemEdit.vue

* Implement effect selection

* Update spells.ts

* Item editor effects adjustments

* Update ItemEdit.vue

* Update ItemEdit.vue

* Delete eq-spell-spa-definitions.ts

* Update spells.ts

bard effect update

* Update spells.ts

* Update spells.ts

* Update spells.ts

* Add new color picker

* type descriptions

defines added for type descriptions

* Update eq-spell-constants.ts

* Update spells.ts

* Updated DB_SPA list

Improved to many descriptions.

* Update eq-spell-constants.ts

* update 288

needs more work to pull AA spell data

* Adjust sizing

* Adjust sizes

* Add checkbox hover

* Additional field work

* More fields

* Add additional top level tags to card preview

* More fields

* More field work

* Restructure files in project

* More project cleanup

* Restructure for spell editor

* Move viewers to their own folder

* Display item cash

* Add margin to bottom of preview card

* Display light of item

* Update ItemEditor.vue

* Hook up stat scaling

* Tweak scaling

* Add stack size to preview

* Verbiage change

* Add meta tab to items

* Add tab hover local setting

* Change spacing

* Pre-highlight fields being scaled on hover

* Adjust highlight effect colors

* Add percentage based stat scalers

* Refactoring

* Implement ItemStatScalePercentage component

* More field work

* Update ItemEditor.vue

* Update ItemEditor.vue

* Tweaks

* Modified fields now breathe

* Update global.css

* Update ItemEditor.vue

* Tooltips

* Tooltips

* Update main.ts

* Implement color selector

* Add bane race field select

* UI tweaks

* Add material preview

* Material

* Material tweaks

* Add bag type dropdown

* Update EQItemCardPreview.vue

* Tweaks

* Update eq-ui.css

* Add selects of data, adjust preview

* Update ItemEditor.vue

* Add purity

* Add augment type calculator

* Relocate spell damage

* Bag weight reduction

* Add elite material and hero forge model

* Update ItemEditor.vue

* Book fields

* Charm

* Last remaining fields

* Tooltip hover descriptions

* More tooltip description work

* Aug description

* Update ItemEditor.vue

* Little bit of spell field rearranging

* Hook up tooltip description data

* Create eq-spell-spa-definitions.ts

* SPA preview work

* Spell table preview cleanup

* More ui cleanup

* Restrictions tab work

* Cleanup buffing tab

* Casting tab

* Move pcnpc_only_flag

* General tab

* Resist tab

* Effects+ tab

* Range tab

* Tweaks to spell table

* Add additional filters to item search

* Update Items.vue

* Implement additional item filters

* More item table preview work

* More ui work

* Update ItemPreviewTable.vue

* UI tweaks

* UI tweaks to spells table

* More ui tweaks

* Loader tweaks

* Pre editor changes

* More quest editor work

* Remove vue form generation

* Remove no longer used assets

* More adjustments

* List files endpoint

* More quest editor prototype work

* Update ItemPreviewTable.vue

* More editor work

* updates to effect descriptions

* Prep for release

Co-authored-by: KayenEQ <kayen85@gmail.com>
@bencq
Copy link

bencq commented Jun 14, 2022

Select("*").Omit("Name", "Age")

It doesn't work. Select("*") still ignores all zero values.

Yes, I also encountered this problem. Select("*") before Update() doesn't work.

@0x9ef
Copy link

0x9ef commented Feb 7, 2023

I've faced the same trouble.

I guess it would be better to implement smth like gorm:"allowzero" tag. Why? There are 2 approaches to force zero fields. The first is to use a pointer and the second is to convert the structure to a map. These approaches have critical issues in my opinion.

Approach with a pointer:

  • We may don't want to use a pointer because it allows using nil as a value, in this way we will generate redundant checks for nil

Approach with converting a structure to a map:

  • It will provide a performance overhead. Because if a structure is too complex, in large production it would create unacceptable overhead costs

But, if we want to update an association with zero-value it will work.

@enzofoucaud
Copy link

Any update ?

Select("*") don't work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:question general questions
Projects
None yet
Development

No branches or pull requests

6 participants