File: tag.go

package info (click to toggle)
golang-github-cavaliergopher-rpm 1.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 704 kB
  • sloc: python: 84; makefile: 4
file content (116 lines) | stat: -rw-r--r-- 2,497 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package rpm

const (
	TagTypeNull TagType = iota
	TagTypeChar
	TagTypeInt8
	TagTypeInt16
	TagTypeInt32
	TagTypeInt64
	TagTypeString
	TagTypeBinary
	TagTypeStringArray
	TagTypeI18NString
)

var tagTypeNames = []string{
	"NULL",
	"CHAR",
	"INT8",
	"INT16",
	"INT32",
	"INT64",
	"STRING",
	"BIN",
	"STRING_ARRAY",
	"I18NSTRING",
}

// TagType describes the data type of a tag's value.
type TagType int

func (i TagType) String() string {
	if i > 0 && int(i) < len(tagTypeNames) {
		return tagTypeNames[i]
	}
	return "UNKNOWN"
}

// Tag is an rpm header entry and its associated data value. Once the data type
// is known, use the associated value method to retrieve the tag value.
//
// All Tag methods will return their zero value if the underlying data type is
// a different type or if the tag is nil.
type Tag struct {
	ID    int
	Type  TagType
	Value interface{}
}

// StringSlice returns a slice of strings or nil if the index is not a string
// slice value.
//
// Use StringSlice for all STRING, STRING_ARRAY and I18NSTRING data types.
func (c *Tag) StringSlice() []string {
	if c == nil || c.Value == nil {
		return nil
	}
	if v, ok := c.Value.([]string); ok {
		return v
	}
	return nil
}

// String returns a string or an empty string if the index is not a string
// value.
//
// Use String for all STRING, STRING_ARRAY and I18NSTRING data types.
//
// This is not intended to implement fmt.Stringer. To format the tag using its
// identifier, use Tag.ID. To format the tag's value, use Tag.Value.
func (c *Tag) String() string {
	v := c.StringSlice()
	if len(v) == 0 {
		return ""
	}
	return v[0]
}

// Int64Slice returns a slice of int64s or nil if the index is not a numerical
// slice value. All integer types are cast to int64.
//
// Use Int64Slice for all INT16, INT32 and INT64 data types.
func (c *Tag) Int64Slice() []int64 {
	if c == nil || c.Value == nil {
		return nil
	}
	if v, ok := c.Value.([]int64); ok {
		return v
	}
	return nil
}

// Int64 returns an int64 if the index is not a numerical value. All integer
// types are cast to int64.
//
// Use Int64 for all INT16, INT32 and INT64 data types.
func (c *Tag) Int64() int64 {
	v := c.Int64Slice()
	if len(v) > 0 {
		return v[0]
	}
	return 0
}

// Bytes returns a slice of bytes or nil if the index is not a byte slice value.
//
// Use Bytes for all CHAR, INT8 and BIN data types.
func (c *Tag) Bytes() []byte {
	if c == nil || c.Value == nil {
		return nil
	}
	if v, ok := c.Value.([]byte); ok {
		return v
	}
	return nil
}