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

     1  package datamodel
     2  
     3  // Kind represents the primitive kind in the IPLD data model.
     4  // All of these kinds map directly onto serializable data.
     5  //
     6  // Note that Kind contains the concept of "map", but not "struct"
     7  // or "object" -- those are a concepts that could be introduced in a
     8  // type system layers, but are *not* present in the data model layer,
     9  // and therefore they aren't included in the Kind enum.
    10  type Kind uint8
    11  
    12  const (
    13  	Kind_Invalid Kind = 0
    14  	Kind_Map     Kind = '{'
    15  	Kind_List    Kind = '['
    16  	Kind_Null    Kind = '0'
    17  	Kind_Bool    Kind = 'b'
    18  	Kind_Int     Kind = 'i'
    19  	Kind_Float   Kind = 'f'
    20  	Kind_String  Kind = 's'
    21  	Kind_Bytes   Kind = 'x'
    22  	Kind_Link    Kind = '/'
    23  )
    24  
    25  func (k Kind) String() string {
    26  	switch k {
    27  	case Kind_Invalid:
    28  		return "INVALID"
    29  	case Kind_Map:
    30  		return "map"
    31  	case Kind_List:
    32  		return "list"
    33  	case Kind_Null:
    34  		return "null"
    35  	case Kind_Bool:
    36  		return "bool"
    37  	case Kind_Int:
    38  		return "int"
    39  	case Kind_Float:
    40  		return "float"
    41  	case Kind_String:
    42  		return "string"
    43  	case Kind_Bytes:
    44  		return "bytes"
    45  	case Kind_Link:
    46  		return "link"
    47  	default:
    48  		panic("invalid enumeration value!")
    49  	}
    50  }
    51  
    52  // KindSet is a type with a few enumerated consts that are commonly used
    53  // (mostly, in error messages).
    54  type KindSet []Kind
    55  
    56  var (
    57  	KindSet_Recursive = KindSet{Kind_Map, Kind_List}
    58  	KindSet_Scalar    = KindSet{Kind_Null, Kind_Bool, Kind_Int, Kind_Float, Kind_String, Kind_Bytes, Kind_Link}
    59  
    60  	KindSet_JustMap    = KindSet{Kind_Map}
    61  	KindSet_JustList   = KindSet{Kind_List}
    62  	KindSet_JustNull   = KindSet{Kind_Null}
    63  	KindSet_JustBool   = KindSet{Kind_Bool}
    64  	KindSet_JustInt    = KindSet{Kind_Int}
    65  	KindSet_JustFloat  = KindSet{Kind_Float}
    66  	KindSet_JustString = KindSet{Kind_String}
    67  	KindSet_JustBytes  = KindSet{Kind_Bytes}
    68  	KindSet_JustLink   = KindSet{Kind_Link}
    69  )
    70  
    71  func (x KindSet) String() string {
    72  	if len(x) == 0 {
    73  		return "<empty KindSet>"
    74  	}
    75  	s := ""
    76  	for i := 0; i < len(x)-1; i++ {
    77  		s += x[i].String() + " or "
    78  	}
    79  	s += x[len(x)-1].String()
    80  	return s
    81  }
    82  
    83  func (x KindSet) Contains(e Kind) bool {
    84  	for _, v := range x {
    85  		if v == e {
    86  			return true
    87  		}
    88  	}
    89  	return false
    90  }