library: io: expose ID, User and Config via methods

These fields can be copied safely and is useful information outside the library, so they are exposed via methods that copy their value.

Signed-off-by: Ophestra Umiker <cat@ophivana.moe>
This commit is contained in:
Ophestra Umiker 2024-06-29 17:48:32 +09:00
parent 8a975f9a31
commit b3938173ae
Signed by: cat
SSH Key Fingerprint: SHA256:gQ67O0enBZ7UdZypgtspB2FDM1g3GVw8nX0XSdcFw8Q
1 changed files with 27 additions and 2 deletions

29
io.go
View File

@ -21,6 +21,32 @@ type Client struct {
conn net.Conn
}
// ID returns the Client Application ID nil-safely
func (d *Client) ID() string {
if d == nil {
return ""
}
return d.id
}
// User returns the Client User nil-safely
func (d *Client) User() (User, bool) {
if d == nil || d.user == nil {
return User{}, false
}
return *d.user, true
}
// Config returns the Client Config nil-safely
func (d *Client) Config() (Config, bool) {
if d == nil || d.config == nil {
return Config{}, false
}
return *d.config, true
}
// Raw wraps around the Raw method to provide generic json parsing
func Raw[T any](d *Client, opcode uint32, payload any) (uint32, T, error) {
var p T
@ -82,8 +108,7 @@ func (d *Client) raw(opcode uint32, payload any) (uint32, []byte, error) {
// Close the client, this is required before exit
func (d *Client) Close() error {
if !d.dialed {
// silently succeed because client activation is implicit
if d == nil || !d.dialed {
return nil
}