示例#1
0
// AtIndex gets the value at the given index
func (r *CSVRecord) AtIndex(index int) (*ch.Const, error) {

	if index < 0 || index > len(r.record) {
		return nil, fmt.Errorf("index out of bounds %d", index)
	}

	// FIXME should we accept NULL values?
	value := r.record[index]
	if value == "NULL" {
		return ch.NullConst(), nil
	}

	return ch.ConstFromString(value), nil
}
示例#2
0
func jsonToConst(partial *json.RawMessage) (*ch.Const, error) {
	var value string

	if partial == nil {
		return ch.NullConst(), nil
	}

	asString := string(*partial)

	if asString == "null" {
		return ch.NullConst(), nil
	}

	if err := json.Unmarshal(*partial, &value); err != nil {
		if err, ok := err.(*json.UnmarshalTypeError); ok {
			// we failed to unmarshal into a string, let's try the other types
			switch err.Value {
			case "number":
				var n json.Number
				if err := json.Unmarshal(*partial, &n); err != nil {
					return nil, err
				}

				value = n.String()

			case "bool":
				value = asString

			default:
				return nil, err
			}
		}
	}

	return ch.ConstFromString(value), nil
}