github.com/ipld/go-ipld-prime@v0.21.0/datamodel/unit.go (about)

     1  package datamodel
     2  
     3  // Null is the one kind of node we can have a true singleton implementation of.
     4  // This is that value.
     5  // The Null Node has Kind() == Kind_Null, returns IsNull() == true,
     6  // returns ErrWrongKind to most other inquiries (as you'd expect),
     7  // and returns a NodePrototype with a NewBuilder method that simply panics
     8  // (because why would you ever try to build a new "null"?).
     9  var Null Node = nullNode{}
    10  
    11  type nullNode struct{}
    12  
    13  func (nullNode) Kind() Kind {
    14  	return Kind_Null
    15  }
    16  func (nullNode) LookupByString(key string) (Node, error) {
    17  	return nil, ErrWrongKind{TypeName: "null", MethodName: "LookupByString", AppropriateKind: KindSet_JustMap, ActualKind: Kind_Null}
    18  }
    19  func (nullNode) LookupByNode(key Node) (Node, error) {
    20  	return nil, ErrWrongKind{TypeName: "null", MethodName: "LookupByNode", AppropriateKind: KindSet_JustMap, ActualKind: Kind_Null}
    21  }
    22  func (nullNode) LookupByIndex(idx int64) (Node, error) {
    23  	return nil, ErrWrongKind{TypeName: "null", MethodName: "LookupByIndex", AppropriateKind: KindSet_JustList, ActualKind: Kind_Null}
    24  }
    25  func (nullNode) LookupBySegment(seg PathSegment) (Node, error) {
    26  	return nil, ErrWrongKind{TypeName: "null", MethodName: "LookupBySegment", AppropriateKind: KindSet_Recursive, ActualKind: Kind_Null}
    27  }
    28  func (nullNode) MapIterator() MapIterator {
    29  	return nil
    30  }
    31  func (nullNode) ListIterator() ListIterator {
    32  	return nil
    33  }
    34  func (nullNode) Length() int64 {
    35  	return -1
    36  }
    37  func (nullNode) IsAbsent() bool {
    38  	return false
    39  }
    40  func (nullNode) IsNull() bool {
    41  	return true
    42  }
    43  func (nullNode) AsBool() (bool, error) {
    44  	return false, ErrWrongKind{TypeName: "null", MethodName: "AsBool", AppropriateKind: KindSet_JustBool, ActualKind: Kind_Null}
    45  }
    46  func (nullNode) AsInt() (int64, error) {
    47  	return 0, ErrWrongKind{TypeName: "null", MethodName: "AsInt", AppropriateKind: KindSet_JustInt, ActualKind: Kind_Null}
    48  }
    49  func (nullNode) AsFloat() (float64, error) {
    50  	return 0, ErrWrongKind{TypeName: "null", MethodName: "AsFloat", AppropriateKind: KindSet_JustFloat, ActualKind: Kind_Null}
    51  }
    52  func (nullNode) AsString() (string, error) {
    53  	return "", ErrWrongKind{TypeName: "null", MethodName: "AsString", AppropriateKind: KindSet_JustString, ActualKind: Kind_Null}
    54  }
    55  func (nullNode) AsBytes() ([]byte, error) {
    56  	return nil, ErrWrongKind{TypeName: "null", MethodName: "AsBytes", AppropriateKind: KindSet_JustBytes, ActualKind: Kind_Null}
    57  }
    58  func (nullNode) AsLink() (Link, error) {
    59  	return nil, ErrWrongKind{TypeName: "null", MethodName: "AsLink", AppropriateKind: KindSet_JustLink, ActualKind: Kind_Null}
    60  }
    61  func (nullNode) Prototype() NodePrototype {
    62  	return nullPrototype{}
    63  }
    64  
    65  type nullPrototype struct{}
    66  
    67  func (nullPrototype) NewBuilder() NodeBuilder {
    68  	panic("cannot build null nodes")
    69  }
    70  
    71  // Absent is the _other_ kind of node (besides Null) we can have a true singleton implementation of.
    72  // This is the singleton value for Absent.
    73  // The Absent Node has Kind() == Kind_Null, returns IsNull() == false (!),
    74  // returns IsAbsent() == true,
    75  // returns ErrWrongKind to most other inquiries (as you'd expect),
    76  // and returns a NodePrototype with a NewBuilder method that simply panics
    77  // (because why would you ever try to build a new "nothing"?).
    78  //
    79  // Despite its presence in the datamodel package, "absent" is not really a data model concept.
    80  // Absent should not really be seen in any datamodel Node implementations, for example.
    81  // Absent is seen used in the realm of schemas and typed data, because there,
    82  // there's a concept of data that has been described, and yet is not currently present;
    83  // it is this concept that "absent" is used to express.
    84  // Absent also sometimes shows up as a distinct case in codecs or other diagnostic printing,
    85  // and suchlike miscellaneous places; it is for these reasons that it's here in the datamodel package,
    86  // because it would end up imported somewhat universally for those diagnostic purposes anyway.
    87  // (This may be worth a design review at some point, but holds up well enough for now.)
    88  var Absent Node = absentNode{}
    89  
    90  type absentNode struct{}
    91  
    92  func (absentNode) Kind() Kind {
    93  	return Kind_Null
    94  }
    95  func (absentNode) LookupByString(key string) (Node, error) {
    96  	return nil, ErrWrongKind{TypeName: "absent", MethodName: "LookupByString", AppropriateKind: KindSet_JustMap, ActualKind: Kind_Null}
    97  }
    98  func (absentNode) LookupByNode(key Node) (Node, error) {
    99  	return nil, ErrWrongKind{TypeName: "absent", MethodName: "LookupByNode", AppropriateKind: KindSet_JustMap, ActualKind: Kind_Null}
   100  }
   101  func (absentNode) LookupByIndex(idx int64) (Node, error) {
   102  	return nil, ErrWrongKind{TypeName: "absent", MethodName: "LookupByIndex", AppropriateKind: KindSet_JustList, ActualKind: Kind_Null}
   103  }
   104  func (absentNode) LookupBySegment(seg PathSegment) (Node, error) {
   105  	return nil, ErrWrongKind{TypeName: "absent", MethodName: "LookupBySegment", AppropriateKind: KindSet_Recursive, ActualKind: Kind_Null}
   106  }
   107  func (absentNode) MapIterator() MapIterator {
   108  	return nil
   109  }
   110  func (absentNode) ListIterator() ListIterator {
   111  	return nil
   112  }
   113  func (absentNode) Length() int64 {
   114  	return -1
   115  }
   116  func (absentNode) IsAbsent() bool {
   117  	return true
   118  }
   119  func (absentNode) IsNull() bool {
   120  	return false
   121  }
   122  func (absentNode) AsBool() (bool, error) {
   123  	return false, ErrWrongKind{TypeName: "absent", MethodName: "AsBool", AppropriateKind: KindSet_JustBool, ActualKind: Kind_Null}
   124  }
   125  func (absentNode) AsInt() (int64, error) {
   126  	return 0, ErrWrongKind{TypeName: "absent", MethodName: "AsInt", AppropriateKind: KindSet_JustInt, ActualKind: Kind_Null}
   127  }
   128  func (absentNode) AsFloat() (float64, error) {
   129  	return 0, ErrWrongKind{TypeName: "absent", MethodName: "AsFloat", AppropriateKind: KindSet_JustFloat, ActualKind: Kind_Null}
   130  }
   131  func (absentNode) AsString() (string, error) {
   132  	return "", ErrWrongKind{TypeName: "absent", MethodName: "AsString", AppropriateKind: KindSet_JustString, ActualKind: Kind_Null}
   133  }
   134  func (absentNode) AsBytes() ([]byte, error) {
   135  	return nil, ErrWrongKind{TypeName: "absent", MethodName: "AsBytes", AppropriateKind: KindSet_JustBytes, ActualKind: Kind_Null}
   136  }
   137  func (absentNode) AsLink() (Link, error) {
   138  	return nil, ErrWrongKind{TypeName: "absent", MethodName: "AsLink", AppropriateKind: KindSet_JustLink, ActualKind: Kind_Null}
   139  }
   140  func (absentNode) Prototype() NodePrototype {
   141  	return absentPrototype{}
   142  }
   143  
   144  type absentPrototype struct{}
   145  
   146  func (absentPrototype) NewBuilder() NodeBuilder {
   147  	panic("cannot build absent nodes")
   148  }