This file is indexed.

/usr/share/gocode/src/pault.ag/go/debian/control/encode.go is in golang-pault-go-debian-dev 0.5-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/* {{{ Copyright (c) Paul R. Tagliamonte <paultag@debian.org>, 2015
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE. }}} */

package control

import (
	"fmt"
	"io"
	"reflect"
	"strconv"
	"strings"
)

// Marshallable {{{

// The Marshallable interface defines the interface that Marshal will use
// to do custom dehydration of the Struct back into the Debian 822 format.
type Marshallable interface {
	MarshalControl() (string, error)
}

// }}}

// ConvertToParagraph {{{

// Given a Struct, convert that Struct back into a control.Paragraph.
// This is not exactly useful as part of the external API, but may be
// useful in some funny circumstances where you need to treat a Struct
// you Unmarshaled into as a control.Paragraph again.
//
// In most cases, the Marshal API should be sufficient. Use of this API
// is mildly discouraged.
func ConvertToParagraph(incoming interface{}) (*Paragraph, error) {
	data := reflect.ValueOf(incoming)
	if data.Type().Kind() != reflect.Ptr {
		return nil, fmt.Errorf("Can only Decode a pointer to a Struct")
	}
	return convertToParagraph(data.Elem())
}

// Top-level conversion dispatch {{{

func convertToParagraph(data reflect.Value) (*Paragraph, error) {
	order := []string{}
	values := map[string]string{}

	if data.Type().Kind() != reflect.Struct {
		return nil, fmt.Errorf("Can only Decode a Struct")
	}

	paragraphType := reflect.TypeOf(Paragraph{})
	var foundParagraph Paragraph = Paragraph{}

	for i := 0; i < data.NumField(); i++ {
		field := data.Field(i)
		fieldType := data.Type().Field(i)

		if fieldType.Anonymous {
			if fieldType.Type == paragraphType {
				foundParagraph = field.Interface().(Paragraph)
			}
			continue
		}

		paragraphKey := fieldType.Name
		if it := fieldType.Tag.Get("control"); it != "" {
			paragraphKey = it
		}

		if paragraphKey == "-" {
			/* If the key is "-", lets go ahead and skip it */
			continue
		}

		data, err := marshalStructValue(field, fieldType)
		if err != nil {
			return nil, err
		}

		required := fieldType.Tag.Get("required") == "true"
		if data == "" && !required {
			continue
		}

		if fieldType.Tag.Get("multiline") == "true" {
			data = "\n" + data
		}

		order = append(order, paragraphKey)
		values[paragraphKey] = data
	}
	para := foundParagraph.Update(Paragraph{Order: order, Values: values})
	return &para, nil
}

// }}}

// convert a struct value {{{

func marshalStructValue(field reflect.Value, fieldType reflect.StructField) (string, error) {
	switch field.Type().Kind() {
	case reflect.String:
		return field.String(), nil
	case reflect.Uint:
		return strconv.Itoa(int(field.Uint())), nil
	case reflect.Int:
		return strconv.Itoa(int(field.Int())), nil
	case reflect.Ptr:
		return marshalStructValue(field.Elem(), fieldType)
	case reflect.Slice:
		return marshalStructValueSlice(field, fieldType)
	case reflect.Struct:
		return marshalStructValueStruct(field, fieldType)
	}
	return "", fmt.Errorf("Unknown type: %s", field.Type().Kind())
}

// }}}

// convert a struct value of type struct {{{

func marshalStructValueStruct(field reflect.Value, fieldType reflect.StructField) (string, error) {
	/* Right, so, we've got a type we don't know what to do with. We should
	 * grab the method, or throw a shitfit. */
	if marshal, ok := field.Interface().(Marshallable); ok {
		return marshal.MarshalControl()
	}

	return "", fmt.Errorf(
		"Type '%s' does not implement control.Marshallable",
		field.Type().Name(),
	)
}

// }}}

// convert a struct value of type slice {{{

func marshalStructValueSlice(field reflect.Value, fieldType reflect.StructField) (string, error) {
	var delim = " "
	if it := fieldType.Tag.Get("delim"); it != "" {
		delim = it
	}
	data := []string{}

	for i := 0; i < field.Len(); i++ {
		elem := field.Index(i)
		if stringification, err := marshalStructValue(elem, fieldType); err != nil {
			return "", err
		} else {
			data = append(data, stringification)
		}
	}

	return strings.Join(data, delim), nil
}

// }}}

// }}}

// Marshal {{{

// Marshal is a one-off interface to serialize a single object to a writer.
//
// Most notably, this will *not* separate Paragraphs with a newline as is
// expected upon repeated calls, please use the Encoder streaming interface
// for that.
//
// It's also worth noting that this *will* also write out elements that
// were Unmarshaled into a Struct without a member of that name if (and only
// if) the target Struct contains a `control.Paragraph` anonymous member.
//
// This is handy if the Unmarshaler was given any `X-*` keys that were not
// present on your Struct.
//
// Given a struct (or list of structs), write to the io.Writer stream
// in the RFC822-alike Debian control-file format
//
// This code will attempt to unpack it into the struct based on the
// literal name of the key, This can be overridden by the struct tag
// `control:""`.
//
// If you're dehydrating a list of strings, you have the option of defining
// a string to join the tokens with (`delim:", "`).
//
// In order to Marshal a custom Struct, you are required to implement the
// Marshallable interface. It's highly encouraged to put this interface on
// the struct without a pointer receiver, so that pass-by-value works
// when you call Marshal.
func Marshal(writer io.Writer, data interface{}) error {
	encoder, err := NewEncoder(writer)
	if err != nil {
		return err
	}
	return encoder.Encode(data)
}

// }}}

// Encoder {{{

// Encoder is a struct that allows for the streaming Encoding of data
// back out to an `io.Writer`. Most notably, this will separate
// subsequent `Encode` calls of a Struct with a newline.
//
// It's also worth noting that this *will* also write out elements that
// were Unmarshaled into a Struct without a member of that name if (and only
// if) the target Struct contains a `control.Paragraph` anonymous member.
//
// This is handy if the Unmarshaler was given any `X-*` keys that were not
// present on your Struct.
//
// Given a struct (or list of structs), write to the io.Writer stream
// in the RFC822-alike Debian control-file format
//
// This code will attempt to unpack it into the struct based on the
// literal name of the key, This can be overridden by the struct tag
// `control:""`.
//
// If you're dehydrating a list of strings, you have the option of defining
// a string to join the tokens with (`delim:", "`).
//
// In order to Marshal a custom Struct, you are required to implement the
// Marshallable interface. It's highly encouraged to put this interface on
// the struct without a pointer receiver, so that pass-by-value works
// when you call Marshal.
type Encoder struct {
	writer         io.Writer
	alreadyWritten bool
}

// NewEncoder {{{

// Create a new Encoder, which is configured to write to the given `io.Writer`.
func NewEncoder(writer io.Writer) (*Encoder, error) {
	return &Encoder{
		writer:         writer,
		alreadyWritten: false,
	}, nil
}

// }}}

// Encode {{{

// Take a Struct, Encode it into a Paragraph, and write that out to the
// io.Writer set up when the Encoder was configured.
func (e *Encoder) Encode(incoming interface{}) error {
	data := reflect.ValueOf(incoming)
	return e.encode(data)
}

// Top-level Encode reflect dispatch {{{

func (e *Encoder) encode(data reflect.Value) error {
	if data.Type().Kind() == reflect.Ptr {
		return e.encode(data.Elem())
	}

	switch data.Type().Kind() {
	case reflect.Slice:
		return e.encodeSlice(data)
	case reflect.Struct:
		return e.encodeStruct(data)
	}
	return fmt.Errorf("Unknown type")
}

// }}}

// Encode a Slice {{{

func (e *Encoder) encodeSlice(data reflect.Value) error {
	for i := 0; i < data.Len(); i++ {
		if err := e.encodeStruct(data.Index(i)); err != nil {
			return err
		}
	}
	return nil
}

// }}}

// Encode a Struct {{{

func (e *Encoder) encodeStruct(data reflect.Value) error {
	if e.alreadyWritten {
		_, err := e.writer.Write([]byte("\n"))
		if err != nil {
			return err
		}
	}
	paragraph, err := convertToParagraph(data)
	if err != nil {
		return err
	}
	e.alreadyWritten = true
	return paragraph.WriteTo(e.writer)
}

// }}}

// }}}

// }}}

// vim: foldmethod=marker