Skip to content

Commit 72a648f

Browse files
committed
backend/pipkey: PipMsg to Gostr conv funcs
1 parent 42414ce commit 72a648f

1 file changed

Lines changed: 21 additions & 0 deletions

File tree

intra/backend/ipn_pipkeygen.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,18 @@ type PipToken Gostr
7474
// - next 32 bytes as client identifier (random)
7575
type PipMsg Gostr
7676

77+
// AsPipMsg typecast Gostr m to PipMsg.
78+
// m must be a 64 bytes hex string
79+
// (32b for msg + 32b for opaque-id).
80+
// Returns nil if the string m is nil or not a valid PipMsg.
81+
func AsPipMsg(m *Gostr) *PipMsg {
82+
p := (*PipMsg)(m)
83+
if !p.ok() {
84+
return nil
85+
}
86+
return p
87+
}
88+
7789
// go.dev/play/p/OTMIv7FLtVs
7890
func pipmsgof(m string) *PipMsg {
7991
// 2 chars per byte in hex
@@ -85,6 +97,15 @@ func pipmsgof(m string) *PipMsg {
8597
return &PipMsg{S: m}
8698
}
8799

100+
// Returns empty Gostr if p is nil or invalid PipMsg.
101+
func (p *PipMsg) AsGostr() *Gostr {
102+
if !p.ok() {
103+
return emptyGostr
104+
}
105+
// go.dev/play/p/hPFgE9s9tMP
106+
return (*Gostr)(p)
107+
}
108+
88109
func (p *PipMsg) v() string {
89110
if p == nil {
90111
return ""

0 commit comments

Comments
 (0)