github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/kbfs/libkbfs/readonly_node.go (about)

     1  // Copyright 2018 Keybase Inc. All rights reserved.
     2  // Use of this source code is governed by a BSD
     3  // license that can be found in the LICENSE file.
     4  
     5  package libkbfs
     6  
     7  import (
     8  	"context"
     9  )
    10  
    11  // CtxReadWriteKeyType is the type of the key for overriding read-only
    12  // nodes.
    13  type CtxReadWriteKeyType int
    14  
    15  const (
    16  	// CtxReadWriteKey is a context key to indicate that a read-only
    17  	// node should be treated as read-write.
    18  	CtxReadWriteKey CtxReadWriteKeyType = 1
    19  )
    20  
    21  // ReadonlyNode is a read-only node by default, unless `CtxReadWriteKey`
    22  // has a value set in the context.
    23  type ReadonlyNode struct {
    24  	Node
    25  }
    26  
    27  var _ Node = (*ReadonlyNode)(nil)
    28  
    29  // Readonly implements the Node interface for ReadonlyNode.
    30  func (rn ReadonlyNode) Readonly(ctx context.Context) bool {
    31  	return ctx.Value(CtxReadWriteKey) == nil
    32  }
    33  
    34  // WrapChild implements the Node interface for ReadonlyNode.
    35  func (rn ReadonlyNode) WrapChild(child Node) Node {
    36  	return &ReadonlyNode{rn.Node.WrapChild(child)}
    37  }
    38  
    39  func readonlyWrapper(node Node) Node {
    40  	return &ReadonlyNode{Node: node}
    41  }