| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package libyaml |
| 9 |
|
| 10 |
import ( |
| 11 |
"errors" |
| 12 |
"fmt" |
| 13 |
) |
| 14 |
|
| 15 |
|
| 16 |
type Options struct { |
| 17 |
|
| 18 |
KnownFields bool |
| 19 |
SingleDocument bool |
| 20 |
UniqueKeys bool |
| 21 |
StreamNodes bool |
| 22 |
AllDocuments bool |
| 23 |
|
| 24 |
|
| 25 |
Indent int |
| 26 |
CompactSeqIndent bool |
| 27 |
LineWidth int |
| 28 |
Unicode bool |
| 29 |
Canonical bool |
| 30 |
LineBreak LineBreak |
| 31 |
ExplicitStart bool |
| 32 |
ExplicitEnd bool |
| 33 |
FlowSimpleCollections bool |
| 34 |
QuotePreference QuoteStyle |
| 35 |
|
| 36 |
|
| 37 |
DepthCheck func(depth int, ctx *DepthContext) error |
| 38 |
AliasCheck func(aliasCount, constructCount int) error |
| 39 |
|
| 40 |
|
| 41 |
FromLegacy bool |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
type Option func(*Options) error |
| 46 |
|
| 47 |
|
| 48 |
type DepthKind string |
| 49 |
|
| 50 |
|
| 51 |
const ( |
| 52 |
DepthKindFlow DepthKind = "flow" |
| 53 |
DepthKindBlock DepthKind = "block" |
| 54 |
) |
| 55 |
|
| 56 |
|
| 57 |
type DepthContext struct { |
| 58 |
Kind DepthKind |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
func DefaultDepthCheck(depth int, ctx *DepthContext) error { |
| 64 |
const maxDepth = 10000 |
| 65 |
if depth > maxDepth { |
| 66 |
return fmt.Errorf("exceeded max depth of %d", maxDepth) |
| 67 |
} |
| 68 |
return nil |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
func DefaultAliasCheck(aliasCount, constructCount int) error { |
| 74 |
const ( |
| 75 |
aliasRatioRangeLow = 400000 |
| 76 |
aliasRatioRangeHigh = 4000000 |
| 77 |
aliasRatioRange = float64(aliasRatioRangeHigh - aliasRatioRangeLow) |
| 78 |
) |
| 79 |
if aliasCount <= 100 || constructCount <= 1000 { |
| 80 |
return nil |
| 81 |
} |
| 82 |
var allowed float64 |
| 83 |
switch { |
| 84 |
case constructCount <= aliasRatioRangeLow: |
| 85 |
allowed = 0.99 |
| 86 |
case constructCount >= aliasRatioRangeHigh: |
| 87 |
allowed = 0.10 |
| 88 |
default: |
| 89 |
allowed = 0.99 - 0.89*(float64(constructCount-aliasRatioRangeLow)/aliasRatioRange) |
| 90 |
} |
| 91 |
if float64(aliasCount)/float64(constructCount) > allowed { |
| 92 |
return errors.New("document contains excessive aliasing") |
| 93 |
} |
| 94 |
return nil |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
func WithIndent(indent int) Option { |
| 102 |
return func(o *Options) error { |
| 103 |
if indent < 2 || indent > 9 { |
| 104 |
return errors.New("yaml: indent must be between 2 and 9 spaces") |
| 105 |
} |
| 106 |
o.Indent = indent |
| 107 |
return nil |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
func WithCompactSeqIndent(compact ...bool) Option { |
| 118 |
if len(compact) > 1 { |
| 119 |
return func(o *Options) error { |
| 120 |
return errors.New("yaml: WithCompactSeqIndent accepts at most one argument") |
| 121 |
} |
| 122 |
} |
| 123 |
val := len(compact) == 0 || compact[0] |
| 124 |
return func(o *Options) error { |
| 125 |
o.CompactSeqIndent = val |
| 126 |
return nil |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
func WithKnownFields(knownFields ...bool) Option { |
| 136 |
if len(knownFields) > 1 { |
| 137 |
return func(o *Options) error { |
| 138 |
return errors.New("yaml: WithKnownFields accepts at most one argument") |
| 139 |
} |
| 140 |
} |
| 141 |
val := len(knownFields) == 0 || knownFields[0] |
| 142 |
return func(o *Options) error { |
| 143 |
o.KnownFields = val |
| 144 |
return nil |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
func WithSingleDocument(singleDocument ...bool) Option { |
| 157 |
if len(singleDocument) > 1 { |
| 158 |
return func(o *Options) error { |
| 159 |
return errors.New("yaml: WithSingleDocument accepts at most one argument") |
| 160 |
} |
| 161 |
} |
| 162 |
val := len(singleDocument) == 0 || singleDocument[0] |
| 163 |
return func(o *Options) error { |
| 164 |
o.SingleDocument = val |
| 165 |
return nil |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
func WithStreamNodes(enable ...bool) Option { |
| 187 |
if len(enable) > 1 { |
| 188 |
return func(o *Options) error { |
| 189 |
return errors.New("yaml: WithStreamNodes accepts at most one argument") |
| 190 |
} |
| 191 |
} |
| 192 |
val := len(enable) == 0 || enable[0] |
| 193 |
return func(o *Options) error { |
| 194 |
o.StreamNodes = val |
| 195 |
return nil |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
func WithAllDocuments(all ...bool) Option { |
| 213 |
if len(all) > 1 { |
| 214 |
return func(o *Options) error { |
| 215 |
return errors.New("yaml: WithAllDocuments accepts at most one argument") |
| 216 |
} |
| 217 |
} |
| 218 |
val := len(all) == 0 || all[0] |
| 219 |
return func(o *Options) error { |
| 220 |
o.AllDocuments = val |
| 221 |
return nil |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
func WithLineWidth(width int) Option { |
| 232 |
return func(o *Options) error { |
| 233 |
if width < 0 { |
| 234 |
width = -1 |
| 235 |
} |
| 236 |
o.LineWidth = width |
| 237 |
return nil |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
. |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
func WithUnicode(unicode ...bool) Option { |
| 249 |
if len(unicode) > 1 { |
| 250 |
return func(o *Options) error { |
| 251 |
return errors.New("yaml: WithUnicode accepts at most one argument") |
| 252 |
} |
| 253 |
} |
| 254 |
val := len(unicode) == 0 || unicode[0] |
| 255 |
return func(o *Options) error { |
| 256 |
o.Unicode = val |
| 257 |
return nil |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
func WithUniqueKeys(uniqueKeys ...bool) Option { |
| 270 |
if len(uniqueKeys) > 1 { |
| 271 |
return func(o *Options) error { |
| 272 |
return errors.New("yaml: WithUniqueKeys accepts at most one argument") |
| 273 |
} |
| 274 |
} |
| 275 |
val := len(uniqueKeys) == 0 || uniqueKeys[0] |
| 276 |
return func(o *Options) error { |
| 277 |
o.UniqueKeys = val |
| 278 |
return nil |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
func WithCanonical(canonical ...bool) Option { |
| 291 |
if len(canonical) > 1 { |
| 292 |
return func(o *Options) error { |
| 293 |
return errors.New("yaml: WithCanonical accepts at most one argument") |
| 294 |
} |
| 295 |
} |
| 296 |
val := len(canonical) == 0 || canonical[0] |
| 297 |
return func(o *Options) error { |
| 298 |
o.Canonical = val |
| 299 |
return nil |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
func WithLineBreak(lineBreak LineBreak) Option { |
| 312 |
return func(o *Options) error { |
| 313 |
o.LineBreak = lineBreak |
| 314 |
return nil |
| 315 |
} |
| 316 |
} |
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
func WithExplicitStart(explicit ...bool) Option { |
| 324 |
if len(explicit) > 1 { |
| 325 |
return func(o *Options) error { |
| 326 |
return errors.New("yaml: WithExplicitStart accepts at most one argument") |
| 327 |
} |
| 328 |
} |
| 329 |
val := len(explicit) == 0 || explicit[0] |
| 330 |
return func(o *Options) error { |
| 331 |
o.ExplicitStart = val |
| 332 |
return nil |
| 333 |
} |
| 334 |
} |
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
func WithExplicitEnd(explicit ...bool) Option { |
| 342 |
if len(explicit) > 1 { |
| 343 |
return func(o *Options) error { |
| 344 |
return errors.New("yaml: WithExplicitEnd accepts at most one argument") |
| 345 |
} |
| 346 |
} |
| 347 |
val := len(explicit) == 0 || explicit[0] |
| 348 |
return func(o *Options) error { |
| 349 |
o.ExplicitEnd = val |
| 350 |
return nil |
| 351 |
} |
| 352 |
} |
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
func WithFlowSimpleCollections(flow ...bool) Option { |
| 363 |
if len(flow) > 1 { |
| 364 |
return func(o *Options) error { |
| 365 |
return errors.New("yaml: WithFlowSimpleCollections accepts at most one argument") |
| 366 |
} |
| 367 |
} |
| 368 |
val := len(flow) == 0 || flow[0] |
| 369 |
return func(o *Options) error { |
| 370 |
o.FlowSimpleCollections = val |
| 371 |
return nil |
| 372 |
} |
| 373 |
} |
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
func WithQuotePreference(style QuoteStyle) Option { |
| 391 |
return func(o *Options) error { |
| 392 |
switch style { |
| 393 |
case QuoteSingle, QuoteDouble, QuoteLegacy: |
| 394 |
o.QuotePreference = style |
| 395 |
return nil |
| 396 |
default: |
| 397 |
return fmt.Errorf("invalid QuoteStyle value: %d", style) |
| 398 |
} |
| 399 |
} |
| 400 |
} |
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
func CombineOptions(opts ...Option) Option { |
| 406 |
return func(o *Options) error { |
| 407 |
for _, opt := range opts { |
| 408 |
if err := opt(o); err != nil { |
| 409 |
return err |
| 410 |
} |
| 411 |
} |
| 412 |
return nil |
| 413 |
} |
| 414 |
} |
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
func ApplyOptions(opts ...Option) (*Options, error) { |
| 419 |
o := &Options{ |
| 420 |
Canonical: false, |
| 421 |
LineBreak: LN_BREAK, |
| 422 |
|
| 423 |
|
| 424 |
Indent: 2, |
| 425 |
CompactSeqIndent: true, |
| 426 |
LineWidth: 80, |
| 427 |
Unicode: true, |
| 428 |
UniqueKeys: true, |
| 429 |
|
| 430 |
|
| 431 |
DepthCheck: DefaultDepthCheck, |
| 432 |
AliasCheck: DefaultAliasCheck, |
| 433 |
} |
| 434 |
for _, opt := range opts { |
| 435 |
if err := opt(o); err != nil { |
| 436 |
return nil, err |
| 437 |
} |
| 438 |
} |
| 439 |
return o, nil |
| 440 |
} |
| 441 |
|
| 442 |
|
| 443 |
var DefaultOptions = &Options{ |
| 444 |
Indent: 4, |
| 445 |
LineWidth: -1, |
| 446 |
Unicode: true, |
| 447 |
UniqueKeys: true, |
| 448 |
QuotePreference: QuoteLegacy, |
| 449 |
} |