From b3938173ae570c70c81614e58a0ea3d46d1434e6 Mon Sep 17 00:00:00 2001 From: Ophestra Umiker Date: Sat, 29 Jun 2024 17:48:32 +0900 Subject: [PATCH] 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 --- io.go | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/io.go b/io.go index 39b11ef..ac2cf71 100644 --- a/io.go +++ b/io.go @@ -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 }