コード例 #1
0
ファイル: encode.go プロジェクト: abema/cloth
// SetColumnQualifiers sets column qualifiers of Mutation by Slice.
func SetColumnQualifiers(family string, t time.Time, m *bigtable.Mutation, slice interface{}) (err error) {

	if family == "" {
		err = fmt.Errorf("cloth: family should not be empty")
		return
	}

	s := reflect.ValueOf(slice)
	if s.Kind() != reflect.Slice {
		err = fmt.Errorf("cloth: slice should be type slice")
		return
	}

	if s.Len() == 0 {
		err = fmt.Errorf("cloth: slice should not be empty")
		return
	}

	for i := 0; i < s.Len(); i++ {

		fs := structs.New(s.Index(i).Interface()).Fields()
		if len(fs) == 0 {
			err = fmt.Errorf("cloth: fields are not found, %v", i)
			return
		}

		for _, f := range fs {

			tg := f.Tag(BigtableTagName)
			if tg == "" {
				continue
			}

			ti := GetBigtableTagInfo(tg)
			if ti.Qualifier && !f.IsZero() {
				m.Set(family, fmt.Sprintf("%s", f.Value()), bigtable.Time(t), nil)
			}

		}
	}

	return
}
コード例 #2
0
ファイル: encode.go プロジェクト: abema/cloth
// SetColumns sets columns of Mutation by Struct.
func SetColumns(family string, t time.Time, m *bigtable.Mutation, i interface{}) (err error) {

	if family == "" {
		err = fmt.Errorf("cloth: family should not be empty")
		return
	}

	if i == nil {
		err = fmt.Errorf("cloth: struct should not be nil")
		return
	}

	fs := structs.New(i).Fields()
	if len(fs) == 0 {
		err = fmt.Errorf("cloth: fields are not found, %v", i)
		return
	}

	for _, f := range fs {

		tg := f.Tag(BigtableTagName)
		if tg == "" {
			continue
		}

		ti := GetBigtableTagInfo(tg)
		if ti.Ignore || ti.Column == "" || ti.Omitempty && f.IsZero() {
			continue
		}

		var b []byte
		b, err = getBytes(f)
		if err != nil {
			m = nil
			break
		}

		m.Set(family, ti.Column, bigtable.Time(t), b)
	}

	return
}