github.com/ssube/gitlab-ci-multi-runner@v1.2.1-0.20160607142738-b8d1285632e6/Godeps/_workspace/src/gopkg.in/yaml.v1/encode.go (about) 1 package yaml 2 3 import ( 4 "reflect" 5 "regexp" 6 "sort" 7 "strconv" 8 "strings" 9 "time" 10 ) 11 12 type encoder struct { 13 emitter yaml_emitter_t 14 event yaml_event_t 15 out []byte 16 flow bool 17 } 18 19 func newEncoder() (e *encoder) { 20 e = &encoder{} 21 e.must(yaml_emitter_initialize(&e.emitter)) 22 yaml_emitter_set_output_string(&e.emitter, &e.out) 23 e.must(yaml_stream_start_event_initialize(&e.event, yaml_UTF8_ENCODING)) 24 e.emit() 25 e.must(yaml_document_start_event_initialize(&e.event, nil, nil, true)) 26 e.emit() 27 return e 28 } 29 30 func (e *encoder) finish() { 31 e.must(yaml_document_end_event_initialize(&e.event, true)) 32 e.emit() 33 e.emitter.open_ended = false 34 e.must(yaml_stream_end_event_initialize(&e.event)) 35 e.emit() 36 } 37 38 func (e *encoder) destroy() { 39 yaml_emitter_delete(&e.emitter) 40 } 41 42 func (e *encoder) emit() { 43 // This will internally delete the e.event value. 44 if !yaml_emitter_emit(&e.emitter, &e.event) && e.event.typ != yaml_DOCUMENT_END_EVENT && e.event.typ != yaml_STREAM_END_EVENT { 45 e.must(false) 46 } 47 } 48 49 func (e *encoder) must(ok bool) { 50 if !ok { 51 msg := e.emitter.problem 52 if msg == "" { 53 msg = "Unknown problem generating YAML content" 54 } 55 fail(msg) 56 } 57 } 58 59 func (e *encoder) marshal(tag string, in reflect.Value) { 60 if !in.IsValid() { 61 e.nilv() 62 return 63 } 64 var value interface{} 65 if getter, ok := in.Interface().(Getter); ok { 66 tag, value = getter.GetYAML() 67 tag = longTag(tag) 68 if value == nil { 69 e.nilv() 70 return 71 } 72 in = reflect.ValueOf(value) 73 } 74 switch in.Kind() { 75 case reflect.Interface: 76 if in.IsNil() { 77 e.nilv() 78 } else { 79 e.marshal(tag, in.Elem()) 80 } 81 case reflect.Map: 82 e.mapv(tag, in) 83 case reflect.Ptr: 84 if in.IsNil() { 85 e.nilv() 86 } else { 87 e.marshal(tag, in.Elem()) 88 } 89 case reflect.Struct: 90 e.structv(tag, in) 91 case reflect.Slice: 92 e.slicev(tag, in) 93 case reflect.String: 94 e.stringv(tag, in) 95 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: 96 if in.Type() == durationType { 97 e.stringv(tag, reflect.ValueOf(in.Interface().(time.Duration).String())) 98 } else { 99 e.intv(tag, in) 100 } 101 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: 102 e.uintv(tag, in) 103 case reflect.Float32, reflect.Float64: 104 e.floatv(tag, in) 105 case reflect.Bool: 106 e.boolv(tag, in) 107 default: 108 panic("Can't marshal type: " + in.Type().String()) 109 } 110 } 111 112 func (e *encoder) mapv(tag string, in reflect.Value) { 113 e.mappingv(tag, func() { 114 keys := keyList(in.MapKeys()) 115 sort.Sort(keys) 116 for _, k := range keys { 117 e.marshal("", k) 118 e.marshal("", in.MapIndex(k)) 119 } 120 }) 121 } 122 123 func (e *encoder) structv(tag string, in reflect.Value) { 124 sinfo, err := getStructInfo(in.Type()) 125 if err != nil { 126 panic(err) 127 } 128 e.mappingv(tag, func() { 129 for _, info := range sinfo.FieldsList { 130 var value reflect.Value 131 if info.Inline == nil { 132 value = in.Field(info.Num) 133 } else { 134 value = in.FieldByIndex(info.Inline) 135 } 136 if info.OmitEmpty && isZero(value) { 137 continue 138 } 139 e.marshal("", reflect.ValueOf(info.Key)) 140 e.flow = info.Flow 141 e.marshal("", value) 142 } 143 }) 144 } 145 146 func (e *encoder) mappingv(tag string, f func()) { 147 implicit := tag == "" 148 style := yaml_BLOCK_MAPPING_STYLE 149 if e.flow { 150 e.flow = false 151 style = yaml_FLOW_MAPPING_STYLE 152 } 153 e.must(yaml_mapping_start_event_initialize(&e.event, nil, []byte(tag), implicit, style)) 154 e.emit() 155 f() 156 e.must(yaml_mapping_end_event_initialize(&e.event)) 157 e.emit() 158 } 159 160 func (e *encoder) slicev(tag string, in reflect.Value) { 161 implicit := tag == "" 162 style := yaml_BLOCK_SEQUENCE_STYLE 163 if e.flow { 164 e.flow = false 165 style = yaml_FLOW_SEQUENCE_STYLE 166 } 167 e.must(yaml_sequence_start_event_initialize(&e.event, nil, []byte(tag), implicit, style)) 168 e.emit() 169 n := in.Len() 170 for i := 0; i < n; i++ { 171 e.marshal("", in.Index(i)) 172 } 173 e.must(yaml_sequence_end_event_initialize(&e.event)) 174 e.emit() 175 } 176 177 // isBase60 returns whether s is in base 60 notation as defined in YAML 1.1. 178 // 179 // The base 60 float notation in YAML 1.1 is a terrible idea and is unsupported 180 // in YAML 1.2 and by this package, but these should be marshalled quoted for 181 // the time being for compatibility with other parsers. 182 func isBase60Float(s string) (result bool) { 183 // Fast path. 184 if s == "" { 185 return false 186 } 187 c := s[0] 188 if !(c == '+' || c == '-' || c >= '0' && c <= '9') || strings.IndexByte(s, ':') < 0 { 189 return false 190 } 191 // Do the full match. 192 return base60float.MatchString(s) 193 } 194 195 // From http://yaml.org/type/float.html, except the regular expression there 196 // is bogus. In practice parsers do not enforce the "\.[0-9_]*" suffix. 197 var base60float = regexp.MustCompile(`^[-+]?[0-9][0-9_]*(?::[0-5]?[0-9])+(?:\.[0-9_]*)?$`) 198 199 func (e *encoder) stringv(tag string, in reflect.Value) { 200 var style yaml_scalar_style_t 201 s := in.String() 202 rtag, rs := resolve("", s) 203 if rtag == yaml_BINARY_TAG { 204 if tag == "" || tag == yaml_STR_TAG { 205 tag = rtag 206 s = rs.(string) 207 } else if tag == yaml_BINARY_TAG { 208 fail("explicitly tagged !!binary data must be base64-encoded") 209 } else { 210 fail("cannot marshal invalid UTF-8 data as " + shortTag(tag)) 211 } 212 } 213 if tag == "" && (rtag != yaml_STR_TAG || isBase60Float(s)) { 214 style = yaml_DOUBLE_QUOTED_SCALAR_STYLE 215 } else if strings.Contains(s, "\n") { 216 style = yaml_LITERAL_SCALAR_STYLE 217 } else { 218 style = yaml_PLAIN_SCALAR_STYLE 219 } 220 e.emitScalar(s, "", tag, style) 221 } 222 223 func (e *encoder) boolv(tag string, in reflect.Value) { 224 var s string 225 if in.Bool() { 226 s = "true" 227 } else { 228 s = "false" 229 } 230 e.emitScalar(s, "", tag, yaml_PLAIN_SCALAR_STYLE) 231 } 232 233 func (e *encoder) intv(tag string, in reflect.Value) { 234 s := strconv.FormatInt(in.Int(), 10) 235 e.emitScalar(s, "", tag, yaml_PLAIN_SCALAR_STYLE) 236 } 237 238 func (e *encoder) uintv(tag string, in reflect.Value) { 239 s := strconv.FormatUint(in.Uint(), 10) 240 e.emitScalar(s, "", tag, yaml_PLAIN_SCALAR_STYLE) 241 } 242 243 func (e *encoder) floatv(tag string, in reflect.Value) { 244 // FIXME: Handle 64 bits here. 245 s := strconv.FormatFloat(float64(in.Float()), 'g', -1, 32) 246 switch s { 247 case "+Inf": 248 s = ".inf" 249 case "-Inf": 250 s = "-.inf" 251 case "NaN": 252 s = ".nan" 253 } 254 e.emitScalar(s, "", tag, yaml_PLAIN_SCALAR_STYLE) 255 } 256 257 func (e *encoder) nilv() { 258 e.emitScalar("null", "", "", yaml_PLAIN_SCALAR_STYLE) 259 } 260 261 func (e *encoder) emitScalar(value, anchor, tag string, style yaml_scalar_style_t) { 262 implicit := tag == "" 263 e.must(yaml_scalar_event_initialize(&e.event, []byte(anchor), []byte(tag), []byte(value), implicit, implicit, style)) 264 e.emit() 265 }