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

Hooks? #2

Closed
fluxxu opened this issue Mar 26, 2015 · 1 comment
Closed

Hooks? #2

fluxxu opened this issue Mar 26, 2015 · 1 comment

Comments

@fluxxu
Copy link

fluxxu commented Mar 26, 2015

Do you think hooks fit in structable's design?
For example,
I have a struct

type Object struct {
    Id int `stbl:"id, PRIMARY_KEY, AUTO_INCREMENT"`
    PropsJSON string `stbl:"props_json"`
    CreatedAt time.Time `stbl:"create_at"`
    UpdatedAt time.Time `stbl:"updated_at"`

    Props map[string]string
}

I want CreateAt and UpdatedAt updated and PropsJSON <=> Props get transformed automatically.
My first thought is that if structable supports Hooks I can write following methods:

// called before Insert
func (this *Object) BeforeInsert() {
    this.CreatedAt = time.Now()
}

// called before Update
func (this *Object) BeforeUpdate() {
    this.UpdatedAt = time.Now()
}

// called before Insert and Update
func (this *Object) BeforeSave() {
    _, this.ProsJSON = json.Marshal(&this.Props)
}

// called after Load
func (this *Object) AfterLoad() {
    json.Unmarshal([]byte(this.PropsJSON), &this.Props)
}

Do you think this is a good idea?

Thanks!

@technosophos
Copy link
Member

That is an interesting idea. I could see it being very useful.
Are you thinking we'd implement it with interfaces? Maybe something like:

type BeforeInserter interface {
    func BeforeInsert()
}

Then I guess the save function could easily type-check this and execute BeforeInsert when appropriate.

fluxxu added a commit to fluxxu/structable that referenced this issue Mar 26, 2015
@fluxxu fluxxu mentioned this issue Mar 26, 2015
@fluxxu fluxxu closed this as completed Mar 3, 2020
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

No branches or pull requests

2 participants