summaryrefslogtreecommitdiff
path: root/src/mongo/gotools/src/github.com/mongodb/mongo-tools/vendor/github.com/mongodb/mongo-tools-common/json/iso_date.go
blob: 48410b7e37bdff146539d3284e673b85f6759cf9 (plain)
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
// Copyright (C) MongoDB, Inc. 2014-present.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0

package json

import (
    "fmt"
    "reflect"
)

// Transition functions for recognizing ISODate.
// Adapted from encoding/json/scanner.go.

// stateIS is the state after reading `IS`.
func stateIS(s *scanner, c int) int {
    if c == 'O' {
        s.step = stateISO
        return scanContinue
    }
    return s.error(c, "in literal ISODate (expecting 'O')")
}

// stateISO is the state after reading `ISO`.
func stateISO(s *scanner, c int) int {
    if c == 'D' {
        s.step = stateD
        return scanContinue
    }
    return s.error(c, "in literal ISODate (expecting 'D')")
}

// Decodes a ISODate literal stored in the underlying byte data into v.
func (d *decodeState) storeISODate(v reflect.Value) {
    op := d.scanWhile(scanSkipSpace)
    if op != scanBeginCtor {
        d.error(fmt.Errorf("expected beginning of constructor"))
    }
    args, err := d.ctor("ISODate", []reflect.Type{isoDateType})
    if err != nil {
        d.error(err)
    }
    switch kind := v.Kind(); kind {
    case reflect.Interface:
        v.Set(args[0])
    default:
        d.error(fmt.Errorf("cannot store %v value into %v type", isoDateType, kind))
    }
}