Exemplo n.º 1
0
// Find implements the charlatan.Record interface
func (r *CSVRecord) Find(field *ch.Field) (*ch.Const, error) {

	name := field.Name()

	if name == "*" {
		return ch.StringConst(fmt.Sprintf("%v", r.record)), nil
	}

	// Column index

	if name[0] == '$' {
		index, err := strconv.ParseInt(name[1:], 10, 64)
		if err != nil {
			return nil, fmt.Errorf("Invalid column index %s: %s", name, err)
		}

		return r.AtIndex(int(index))
	}

	// Column name

	index := r.ColumnNameIndex(name)
	if index < 0 {
		return nil, fmt.Errorf("Can't find field name: %s", name)
	}

	return r.AtIndex(index)
}
Exemplo n.º 2
0
// Find implements the charlatan.Record interface
func (r *JSONRecord) Find(field *ch.Field) (*ch.Const, error) {
	var ok bool
	var partial *json.RawMessage
	var name string

	if name = field.Name(); len(name) == 0 {
		return nil, errEmptyField
	}

	// support for "SELECT *"
	if name == "*" {
		b, err := json.Marshal(r.attrs)
		if err != nil {
			return nil, err
		}
		return ch.StringConst(string(b)), nil
	}

	attrs := r.attrs
	parts := strings.Split(name, ".")

	for i, k := range parts {
		partial, ok = attrs[k]

		if !ok {
			if r.SoftMatching {
				return ch.NullConst(), nil
			}

			return nil, fmt.Errorf("Unknown '%s' field (in '%s')", k, field.Name())
		}

		// update the attrs if we need to go deeper
		if i < len(parts)-1 {
			attrs = make(map[string]*json.RawMessage)
			if err := json.Unmarshal(*partial, &attrs); err != nil {
				if r.SoftMatching {
					return ch.NullConst(), nil
				}
				return nil, err
			}
		}
	}

	return jsonToConst(partial)
}