如何重複使用第三方套件中的結構,同時變更單一欄位的編組行為?
Feb 10, 2024 am 08:18 AMphp小編子墨在這裡分享一個關於如何重複使用第三方包中的結構並更改單一欄位編組行為的技巧。當我們使用第三方套件時,有時我們需要對其中的某個欄位進行自訂編組。本文將介紹一個簡單的方法,可以透過繼承和重寫的方式來實現這一目標,既能重複使用原有的結構,又能滿足個人化需求。接下來讓我們一起來看看具體的實作方法吧!
問題內容
假設我想將一個結構編組到 YAML 中,並且該結構已經定義了其所有 YAML 標記,但有一個我想要更改的標記除外。如何在不更改結構本身的情況下更改此單一欄位的行為?假設該結構來自第三方套件。
這是一個要示範的範例,以及我的最佳嘗試。假設 User
結構(及其關聯的 Secret
結構)來自第三方包,因此我們無法修改它們。
package main import ( "fmt" "gopkg.in/yaml.v2" ) type User struct { Email string `yaml:"email"` Password *Secret `yaml:"password"` } type Secret struct { s string } // MarshalYAML implements the yaml.Marshaler interface for Secret. func (sec *Secret) MarshalYAML() (interface{}, error) { if sec != nil { // Replace `"<secret>"` with `sec.s`, and it gets the desired // behavior. But I can't change the Secret struct: return "<secret>", nil } return nil, nil } func (sec *Secret) UnmarshalYAML(unmarshal func(interface{}) error) error { var st string if err := unmarshal(&st); err != nil { return err } sec.s = st return nil } // My best attempt at the solution: type SolutionAttempt struct { User } func (sol *SolutionAttempt) MarshalYAML() (interface{}, error) { res, err := yaml.Marshal( struct { // I don't like having to repeat all these fields from User: Email string `yaml:"email"` Password string `yaml:"password"` }{ Email: sol.User.Email, Password: sol.User.Password.s, }, ) if err != nil { return nil, err } return string(res), nil } func main() { user := &User{} var data = ` email: [email protected] password: asdf ` err := yaml.Unmarshal([]byte(data), user) if err != nil { fmt.Printf("errors! %s", err) return } buf, err := yaml.Marshal(user) if err != nil { fmt.Printf("errors! %s", err) return } // Without touching User or Secret, how can I unmarshall an // instance of User that renders the secret? fmt.Printf("marshalled output:\n%s\n", buf) /////////////////////////////////////////////////////// // attempted solution: /////////////////////////////////////////////////////// sol := &SolutionAttempt{} var data2 = ` user: email: [email protected] password: asdf ` err = yaml.Unmarshal([]byte(data2), sol) if err != nil { fmt.Printf("errors! %s", err) return } buf, err = yaml.Marshal(sol) if err != nil { fmt.Printf("errors! %s", err) return } fmt.Printf("attempted solution marshalled output:\n%s\n", buf) }
登入後複製
這是上述程式碼的 Go Playground 連結:https://go.dev/play/p/ojiPv4ylCEq
解決方法
這根本不可能。
你的「最佳嘗試」就是正確的道路。
以上是如何重複使用第三方套件中的結構,同時變更單一欄位的編組行為?的詳細內容。更多資訊請關注PHP中文網其他相關文章!
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱門文章
擊敗分裂小說需要多長時間?
3 週前
By DDD
倉庫:如何復興隊友
3 週前
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前
By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
1 週前
By 尊渡假赌尊渡假赌尊渡假赌
公眾號網頁更新緩存難題:如何避免版本更新後舊緩存影響用戶體驗?
3 週前
By 王林

熱門文章
擊敗分裂小說需要多長時間?
3 週前
By DDD
倉庫:如何復興隊友
3 週前
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前
By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
1 週前
By 尊渡假赌尊渡假赌尊渡假赌
公眾號網頁更新緩存難題:如何避免版本更新後舊緩存影響用戶體驗?
3 週前
By 王林

熱門文章標籤

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)