]> go.fuhry.dev Git - fsnotify.git/commitdiff
Add {Event,Op}.Has() (#477)
authorMartin Tournoij <martin@arp242.net>
Sat, 30 Jul 2022 15:28:03 +0000 (17:28 +0200)
committerGitHub <noreply@github.com>
Sat, 30 Jul 2022 15:28:03 +0000 (17:28 +0200)
Using:

if event.Op&fsnotify.Create == fsnotify.Create {
}

is overly verbose and awkward; a Has() method makes this much nicer:

if event.Has(fsnotify.Create) {
}

PR #76 was previously rejected; I think that HasCreate() etc. is
overkill, but a Has() method makes the library quite a bit more
convenient IMO.

I added it to both the Event and Op to avoid confusion; with this we can
change the Op field to be private in a future v2.

Fixes #107

README.md
cmd/fsnotify/main.go
fsnotify.go

index 41871a68ab78f7c54d81b896a71aeb26dd184662..96c5f460848d4c719a50afaf49fdfc16fba90386 100644 (file)
--- a/README.md
+++ b/README.md
@@ -49,7 +49,7 @@ func main() {
                                        return
                                }
                                log.Println("event:", event)
-                               if event.Op&fsnotify.Write == fsnotify.Write {
+                               if event.Has(fsnotify.Write) {
                                        log.Println("modified file:", event.Name)
                                }
                        case err, ok := <-watcher.Errors:
index 1b0356ac1fe6b7fa1cc3209469cb38f213eab3a2..b92419ba401fbee9f57deb1181d3c753f4c7c59f 100644 (file)
@@ -42,7 +42,7 @@ func main() {
 
                                i++
                                m := ""
-                               if e.Op&fsnotify.Write == fsnotify.Write {
+                               if e.Has(fsnotify.Write) {
                                        m = "(modified)"
                                }
                                line("%3d %-10s %-10s %q", i, e.Op, m, e.Name)
index 6a305b5cb3b54b3a7bc46c4ab8af52d7be727647..1610d663a35a64efc926ce9067ff35cc282124cd 100644 (file)
@@ -5,7 +5,8 @@
 //go:build !plan9
 // +build !plan9
 
-// Package fsnotify provides a platform-independent interface for file system notifications.
+// Package fsnotify provides a cross-platform interface for file system
+// notifications.
 package fsnotify
 
 import (
@@ -24,6 +25,9 @@ type Event struct {
        Name string
 
        // File operation that triggered the event.
+       //
+       // This is a bitmask as some systems may send multiple operations at once.
+       // Use the Op.Has() or Event.Has() method instead of comparing with ==.
        Op Op
 }
 
@@ -40,30 +44,34 @@ const (
 )
 
 func (op Op) String() string {
-       // Use a builder for efficient string concatenation
-       var builder strings.Builder
-
-       if op&Create == Create {
-               builder.WriteString("|CREATE")
+       var b strings.Builder
+       if op.Has(Create) {
+               b.WriteString("|CREATE")
        }
-       if op&Remove == Remove {
-               builder.WriteString("|REMOVE")
+       if op.Has(Remove) {
+               b.WriteString("|REMOVE")
        }
-       if op&Write == Write {
-               builder.WriteString("|WRITE")
+       if op.Has(Write) {
+               b.WriteString("|WRITE")
        }
-       if op&Rename == Rename {
-               builder.WriteString("|RENAME")
+       if op.Has(Rename) {
+               b.WriteString("|RENAME")
        }
-       if op&Chmod == Chmod {
-               builder.WriteString("|CHMOD")
+       if op.Has(Chmod) {
+               b.WriteString("|CHMOD")
        }
-       if builder.Len() == 0 {
+       if b.Len() == 0 {
                return ""
        }
-       return builder.String()[1:] // Strip leading pipe
+       return b.String()[1:]
 }
 
+// Has reports if this operation has the given operation.
+func (o Op) Has(h Op) bool { return o&h == h }
+
+// Has reports if this event has the given operation.
+func (e Event) Has(op Op) bool { return e.Op.Has(op) }
+
 // String returns a string representation of the event in the form
 // "file: REMOVE|WRITE|..."
 func (e Event) String() string {