Skip to content

Commit d1b4d26

Browse files
committed
lint
1 parent 79ce546 commit d1b4d26

File tree

5 files changed

+69
-39
lines changed

5 files changed

+69
-39
lines changed

.github/workflows/golangci-lint.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
steps:
1919
- uses: actions/checkout@v4
2020
- name: golangci-lint
21-
uses: golangci/golangci-lint-action@v4
21+
uses: golangci/golangci-lint-action@v6
2222
with:
2323
# Optional: version of golangci-lint to use in form of v1.2 or v1.2.3 or `latest` to use the latest version
2424
version: latest

README.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
* [Room](#Room)
2828
* [NewRoom(scope, code) -> *Room](#NewRoom)
2929
* [NewRoomFromId(scope, roomId) -> *Room](#NewRoomFromId)
30+
* [NewRoomFromName(scope, name) -> *Room](#NewRoomFromName)
3031
* [Id() -> uint64](#Id)
3132
* [Scope() -> string](#Scope)
3233
* [HandleEvent(event, handle)](#HandleEvent)
@@ -124,7 +125,7 @@ Key | Type | Default | Description
124125
-------------------- | ------------------ | ------------ | -----------
125126
DefaultTimeout | time.Duration | `0` | Default time-out used when querying ThingsDB. When `0`, no time-out is used.
126127
AutoReconnect | bool | `true` | When `true`, the connection will try to re-connect when a connection is lost.
127-
ReconnectionAttempts | int | `0` | Maximum number of re-connect attempts. When `0`, re-connect will try forever.
128+
ReconnectionAttempts | int | `0` | Maximum number of re-connect attempts. When `0`, re-connect will try forever.
128129
PingInterval | time.Duration | `30s` | Keep-alive ping interval. When `0`, keep-alive will be disabled.
129130
LogCh | chan string | `nil` | Forward logging to this channel. When `nil`, log will be send to `log.Println(..)`.
130131
LogLevel | LogLevelType | `LogWarning` | Log level. Available levels: `LogDebug`, `LogInfo`, `LogWarning` and `LogError`.
@@ -364,6 +365,19 @@ If the room Id unknown, you may want use [NewRoom(..)](#NewRoom) to get the Id f
364365
room := thingsdb.NewRoomFromId("//stuff", 17)
365366
```
366367

368+
369+
### NewRoomFromName
370+
371+
NewRoomFromName creates a new room using the name from a room.
372+
373+
Not every room has a name. The function [set_name]https://docs.thingsdb.io/v1/data-types/room/set_name/ must be used to give a room a name.
374+
*Example:*
375+
376+
```go
377+
// Suppose Collection stuff has a room with name "my_room"
378+
room := thingsdb.NewRoomFromName("//stuff", "my_room")
379+
```
380+
367381
### Id
368382

369383
Id returns the Id of the room.

conn.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ func (conn *Conn) auth() error {
339339
return err
340340
}
341341

342-
return fmt.Errorf("No authentication method available")
342+
return fmt.Errorf("no authentication method available")
343343
}
344344

345345
func (conn *Conn) connect() error {

room.go

Lines changed: 51 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ type Room struct {
1616
// Private
1717
id uint64
1818
code *string
19+
name *string
1920
scope string
2021
conn *Conn
2122
waitJoin chan error
@@ -36,9 +37,8 @@ type Room struct {
3637
//
3738
// Example:
3839
//
39-
// // Suppose Collection stuff has a room (.room)
40-
// room := thingsdb.NewRoom("//stuff", ".room.id();")
41-
//
40+
// // Suppose Collection stuff has a room (.room)
41+
// room := thingsdb.NewRoom("//stuff", ".room.id();")
4242
func NewRoom(scope string, code string) *Room {
4343
room := NewRoomFromId(scope, 0)
4444
room.code = &code
@@ -51,14 +51,14 @@ func NewRoom(scope string, code string) *Room {
5151
//
5252
// Example:
5353
//
54-
// // Suppose Collection stuff has a room with Id 17
55-
// room := thingsdb.NewRoomFromId("//stuff", 17)
56-
//
54+
// // Suppose Collection stuff has a room with Id 17
55+
// room := thingsdb.NewRoomFromId("//stuff", 17)
5756
func NewRoomFromId(scope string, id uint64) *Room {
5857
return &Room{
5958
// Private
6059
id: id,
6160
code: nil,
61+
name: nil,
6262
scope: scope,
6363
conn: nil,
6464
waitJoin: nil,
@@ -74,6 +74,20 @@ func NewRoomFromId(scope string, id uint64) *Room {
7474
}
7575
}
7676

77+
// NewRoom creates a new room using the room name.
78+
//
79+
// Example:
80+
//
81+
// // Suppose Collection stuff has a room (.room)
82+
// room := thingsdb.NewRoomFromName("//stuff", "my_room")
83+
func NewRoomFromName(scope string, name string) *Room {
84+
room := NewRoomFromId(scope, 0)
85+
code := "room(name).id();"
86+
room.code = &code
87+
room.name = &name
88+
return room
89+
}
90+
7791
// Id returns the Id of the room.
7892
//
7993
// > Note: Id() returns `0` when the room was created using `NewRoom(..)` and the room has never been joined.
@@ -90,26 +104,25 @@ func (room *Room) Scope() string {
90104
//
91105
// Example:
92106
//
93-
// func onNewMessage(room *thingsdb.Room, args []interface{}) {
94-
// if len(args) != 1 {
95-
// fmt.Println("Invalid number of arguments")
96-
// return
97-
// }
107+
// func onNewMessage(room *thingsdb.Room, args []interface{}) {
108+
// if len(args) != 1 {
109+
// fmt.Println("Invalid number of arguments")
110+
// return
111+
// }
98112
//
99-
// msg, ok := args[0].(string)
100-
// if !ok {
101-
// fmt.Println("Expecting argument 1 to be of type string")
102-
// return
103-
// }
113+
// msg, ok := args[0].(string)
114+
// if !ok {
115+
// fmt.Println("Expecting argument 1 to be of type string")
116+
// return
117+
// }
104118
//
105-
// fmt.Println(msg) // Just print the message
106-
// }
119+
// fmt.Println(msg) // Just print the message
120+
// }
107121
//
108-
// room = thingsdb.NewRoom("//stuff", ".chatRoom.id();")
109-
//
110-
// // Add event handler for the "new-message" event
111-
// room.HandleEvent("new-message", onNewMessage)
122+
// room = thingsdb.NewRoom("//stuff", ".chatRoom.id();")
112123
//
124+
// // Add event handler for the "new-message" event
125+
// room.HandleEvent("new-message", onNewMessage)
113126
func (room *Room) HandleEvent(event string, handle func(room *Room, args []interface{})) {
114127
room.eventHandlers[event] = handle
115128
}
@@ -123,8 +136,7 @@ func (room *Room) HandleEvent(event string, handle func(room *Room, args []inter
123136
//
124137
// Example:
125138
//
126-
// err := room.Join(conn, thingsdb.DefaultWait)
127-
//
139+
// err := room.Join(conn, thingsdb.DefaultWait)
128140
func (room *Room) Join(conn *Conn, wait time.Duration) error {
129141

130142
if wait > 0 {
@@ -142,7 +154,7 @@ func (room *Room) Join(conn *Conn, wait time.Duration) error {
142154

143155
go func() {
144156
time.Sleep(wait)
145-
room.waitJoin <- fmt.Errorf("Timeout while waiting for the join event on room Id %d", room.id)
157+
room.waitJoin <- fmt.Errorf("timeout while waiting for the join event on room Id %d", room.id)
146158
}()
147159

148160
for {
@@ -159,8 +171,7 @@ func (room *Room) Join(conn *Conn, wait time.Duration) error {
159171
//
160172
// Example:
161173
//
162-
// err := room.Leave()
163-
//
174+
// err := room.Leave()
164175
func (room *Room) Leave() error {
165176
if room.id == 0 {
166177
return fmt.Errorf("Room Id is zero (0), most likely the room has never been joined")
@@ -187,13 +198,12 @@ func (room *Room) Leave() error {
187198
//
188199
// Example:
189200
//
190-
// args := []interface{}{"Just some chat message"}
191-
//
192-
// err := room.Emit(
193-
// "new-message", // Event to emit
194-
// args // Arguments (may be nil)
195-
// );
201+
// args := []interface{}{"Just some chat message"}
196202
//
203+
// err := room.Emit(
204+
// "new-message", // Event to emit
205+
// args // Arguments (may be nil)
206+
// );
197207
func (room *Room) Emit(event string, args []interface{}) error {
198208
if room.conn == nil {
199209
return fmt.Errorf("Room Id %d is not joined", room.id)
@@ -206,10 +216,16 @@ func (room *Room) join(conn *Conn) error {
206216
defer conn.rooms.mux.Unlock()
207217

208218
if room.id == 0 {
219+
var vars map[string]interface{}
209220
if room.code == nil {
210221
return fmt.Errorf("Code or a room Id > 0 is required")
211222
}
212-
val, err := conn.Query(room.scope, *room.code, nil)
223+
if room.name != nil {
224+
vars = map[string]interface{}{
225+
"name": *room.name,
226+
}
227+
}
228+
val, err := conn.Query(room.scope, *room.code, vars)
213229
if err != nil {
214230
return err
215231
}
@@ -229,7 +245,7 @@ func (room *Room) join(conn *Conn) error {
229245
case uint64:
230246
roomId = val
231247
default:
232-
return fmt.Errorf("Expecting code `%s` to return with a room Id (type integer), bot got: %v", *room.code, val)
248+
return fmt.Errorf("expecting code `%s` to return with a room Id (type integer), bot got: %v", *room.code, val)
233249
}
234250

235251
roomIds := []*uint64{&roomId}

version.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package thingsdb
22

33
// Version exposes the go-thingsdb version
4-
const Version = "1.0.6"
4+
const Version = "1.0.7"
55

66
//Publish module:
77
//

0 commit comments

Comments
 (0)