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 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
|
/* {{{ 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 "pault.ag/go/debian/control"
import (
"fmt"
"io"
"reflect"
"strconv"
"strings"
"golang.org/x/crypto/openpgp"
)
// Unmarshallable {{{
// The Unmarshallable interface defines the interface that Unmarshal will use
// to do custom unpacks into Structs.
//
// The argument passed in will be a string that contains the value of the
// RFC822 key this object relates to.
type Unmarshallable interface {
UnmarshalControl(data string) error
}
// }}}
// Unmarshal {{{
// Given a struct (or list of structs), read the io.Reader RFC822-alike
// Debian control-file stream into the struct, unpacking keys into the
// struct as needed. If a list of structs is given, unpack all RFC822
// Paragraphs into the structs.
//
// This code will attempt to unpack it into the struct based on the
// literal name of the key, compared byte-for-byte. If this is not
// OK, the struct tag `control:""` can be used to define the key to use
// in the RFC822 stream.
//
// If you're unpacking into a list of strings, you have the option of defining
// a string to split tokens on (`delim:", "`), and things to strip off each
// element (`strip:"\n\r\t "`).
//
// If you're unpacking into a struct, the struct will be walked according to
// the rules above. If you wish to override how this writes to the nested
// struct, objects that implement the Unmarshallable interface will be
// Unmarshaled via that method call only.
//
// Structs that contain Paragraph as an Anonymous member will have that
// member populated with the parsed RFC822 block, to allow access to the
// .Values and .Order members.
func Unmarshal(data interface{}, reader io.Reader) error {
decoder, err := NewDecoder(reader, nil)
if err != nil {
return err
}
return decoder.Decode(data)
}
// }}}
// Decoder {{{
type Decoder struct {
paragraphReader ParagraphReader
}
// NewDecoder {{{
func NewDecoder(reader io.Reader, keyring *openpgp.EntityList) (*Decoder, error) {
ret := Decoder{}
pr, err := NewParagraphReader(reader, keyring)
if err != nil {
return nil, err
}
ret.paragraphReader = *pr
return &ret, nil
}
// }}}
// Decode {{{
func (d *Decoder) Decode(into interface{}) error {
return decode(&d.paragraphReader, reflect.ValueOf(into))
}
// Top-level decode dispatch {{{
func decode(p *ParagraphReader, into reflect.Value) error {
if into.Type().Kind() != reflect.Ptr {
return fmt.Errorf("Decode can only decode into a pointer!")
}
switch into.Elem().Type().Kind() {
case reflect.Struct:
paragraph, err := p.Next()
if err != nil {
return err
}
return decodeStruct(*paragraph, into)
case reflect.Slice:
return decodeSlice(p, into)
default:
return fmt.Errorf("Can't Decode into a %s", into.Elem().Type().Name())
}
return nil
}
// }}}
// Top-level struct dispatch {{{
func decodeStruct(p Paragraph, into reflect.Value) error {
/* If we have a pointer, let's follow it */
if into.Type().Kind() == reflect.Ptr {
return decodeStruct(p, into.Elem())
}
/* Store the Paragraph type for later use when checking Anonymous
* values. */
paragraphType := reflect.TypeOf(Paragraph{})
/* Right, now, we're going to decode a Paragraph into the struct */
for i := 0; i < into.NumField(); i++ {
field := into.Field(i)
fieldType := into.Type().Field(i)
if field.Type().Kind() == reflect.Struct {
err := decodeStruct(p, field)
if err != nil {
return err
}
}
/* First, let's get the name of the field as we'd index into the
* map[string]string. */
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
}
/* Now, if we have an Anonymous field, we're either going to
* set it to the Paragraph if it's the Paragraph Anonymous member,
* or, more likely, continue through */
if fieldType.Anonymous {
if fieldType.Type == paragraphType {
/* Neat! Let's give the struct this data */
field.Set(reflect.ValueOf(p))
} else {
/* Otherwise, we're going to avoid doing more maths on it */
continue
}
}
if value, ok := p.Values[paragraphKey]; ok {
if err := decodeStructValue(field, fieldType, value); err != nil {
return err
}
continue
} else {
if fieldType.Tag.Get("required") == "true" {
return fmt.Errorf(
"Required field '%s' is missing!",
fieldType.Name,
)
}
continue
}
}
return nil
}
// }}}
// set a struct field value {{{
func decodeStructValue(field reflect.Value, fieldType reflect.StructField, value string) error {
switch field.Type().Kind() {
case reflect.String:
field.SetString(value)
return nil
case reflect.Int:
if value == "" {
field.SetInt(0)
return nil
}
value, err := strconv.Atoi(value)
if err != nil {
return err
}
field.SetInt(int64(value))
return nil
case reflect.Slice:
return decodeStructValueSlice(field, fieldType, value)
case reflect.Struct:
return decodeStructValueStruct(field, fieldType, value)
case reflect.Bool:
field.SetBool(value == "yes")
return nil
}
return fmt.Errorf("Unknown type of field: %s", field.Type())
}
// }}}
// set a struct field value of type struct {{{
func decodeStructValueStruct(incoming reflect.Value, incomingField reflect.StructField, data 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. */
elem := incoming.Addr()
if unmarshal, ok := elem.Interface().(Unmarshallable); ok {
return unmarshal.UnmarshalControl(data)
}
return fmt.Errorf(
"Type '%s' does not implement control.Unmarshallable",
incomingField.Type.Name(),
)
}
// }}}
// set a struct field value of type slice {{{
func decodeStructValueSlice(field reflect.Value, fieldType reflect.StructField, value string) error {
underlyingType := field.Type().Elem()
var delim = " "
if it := fieldType.Tag.Get("delim"); it != "" {
delim = it
}
var strip = ""
if it := fieldType.Tag.Get("strip"); it != "" {
strip = it
}
value = strings.Trim(value, strip)
for _, el := range strings.Split(value, delim) {
el = strings.Trim(el, strip)
targetValue := reflect.New(underlyingType)
err := decodeStructValue(targetValue.Elem(), fieldType, el)
if err != nil {
return err
}
field.Set(reflect.Append(field, targetValue.Elem()))
}
return nil
}
// }}}
// Top-level slice dispatch {{{
func decodeSlice(p *ParagraphReader, into reflect.Value) error {
flavor := into.Elem().Type().Elem()
for {
targetValue := reflect.New(flavor)
/* Get a Paragraph */
para, err := p.Next()
if err == io.EOF {
break
} else if err != nil {
return err
}
if err := decodeStruct(*para, targetValue); err != nil {
return err
}
into.Elem().Set(reflect.Append(into.Elem(), targetValue.Elem()))
}
return nil
}
// }}}
// }}}
// Signer {{{
func (d *Decoder) Signer() *openpgp.Entity {
return d.paragraphReader.Signer()
}
// }}}
// }}}
// UnpackFromParagraph {{{
// Unpack a Paragraph into a Struct, as if that data had been unpacked into
// that struct to begin with. The normal rules from running the Unmarshal
// API directly apply when unpacking a Paragraph using UnpackFromParagraph.
//
// In most cases, the Unmarshal API should be sufficient. Use of this API
// is mildly discouraged.
func UnpackFromParagraph(para Paragraph, incoming interface{}) error {
data := reflect.ValueOf(incoming)
if data.Type().Kind() != reflect.Ptr {
return fmt.Errorf("Can only Decode a pointer to a Struct")
}
return decodeStruct(para, data.Elem())
}
// }}}
// vim: foldmethod=marker
|