Example #1
0
func (ø *Row) SelectByStruct(structPtr interface{}, tagVal string, opts ...interface{}) error {
	str, err := meta.StructByValue(meta.FinalValue(structPtr))
	if err != nil {
		return err
	}
	tags, err2 := str.Tags()
	if err2 != nil {
		return err2
	}
	options := []interface{}{}
	order := []string{}

	for k, v := range tags {
		if t := v.Get("db.select"); t != "" && strings.Contains(t, tagVal) {
			fi := ø.queryField(k, opts...)
			if fi == nil {
				panic("can't find field " + k + " : no Queryfield property of given fields or aliases")
			}
			options = append(options, fi)
			order = append(order, k)
		}
	}

	options = append(options, opts...)
	row, err := ø.Any(options...)
	if err != nil {
		return err
	}
	err = row.GetStruct(tagVal, structPtr)
	if err != nil {
		return err
	}
	return nil
}
Example #2
0
func setFieldInStruct(vl reflect.Value, fieldName string, tagVal string, v *TypedValue, s interface{}) error {

	str, err := meta.StructByValue(meta.FinalValue(s))

	if err != nil {
		return err
	}

	tag, err2 := str.Tag(fieldName)
	if err2 != nil {
		return err2
	}

	// tag does match the given
	if tag != nil && strings.Contains(tag.Get("db.select"), tagVal) {
		err := Convert(v, vl.Addr().Interface())
		if err != nil {
			return fmt.Errorf("error in field %s: %s", fieldName, err.Error())
		}
	}
	return nil
}
Example #3
0
//ro.Get(artist.Id, &a.Id, artist.FirstName, &a.FirstName, artist.LastName, &a.LastName, artist.GalleryArtist, &a.GalleryArtist, artist.Vita, &a.Vita, artist.Area, &ar)
func (ø *Row) GetStruct(tagVal string, s interface{}) (err error) {
	fv := meta.FinalValue(s)

	for f, v := range ø.values {
		if f.queryField == "" {
			panic("queryField not set for " + f.Name)
		}
		// a field with this name does exist
		if vl := fv.FieldByName(f.queryField); vl.IsValid() {
			err = setFieldInStruct(vl, f.queryField, tagVal, v, s)
			if err != nil {
				return
			}
			/*
				tag := meta.Struct.Tag(s, f.queryField)
				// tag does match the given
				if tag != nil && strings.Contains(tag.Get("db.select"), tagVal) {
					err := Convert(v, vl.Addr().Interface())
					if err != nil {
						return fmt.Errorf("error in field %s: %s", f.queryField, err.Error())
					}
				}
			*/
		}
	}

	for f, v := range ø.aliasValues {
		if vl := fv.FieldByName(f.queryField); vl.IsValid() {
			err = setFieldInStruct(vl, f.queryField, tagVal, v, s)
			if err != nil {
				return
			}
			continue
		}
		if vl := fv.FieldByName(f.As); vl.IsValid() {
			err = setFieldInStruct(vl, f.As, tagVal, v, s)
			if err != nil {
				return
			}
			continue
		}
		/*
			if vl := fv.FieldByName(f.As); vl.IsValid() {
				setFieldInStruct(vl, f.As, tagVal, v, s)
					tag := meta.Struct.Tag(s, f.As)
					// tag does match the given
					if tag != nil && strings.Contains(tag.Get("db.select"), tagVal) {
						err := Convert(v, vl.Addr().Interface())
						if err != nil {
							return fmt.Errorf("error in field %s: %s", f.As, err.Error())
						}
					}
			}

		*/
		/*
			else if {
				tag := meta.Struct.Tag(s, f.As)
				// tag does match the given
				if tag != nil && strings.Contains(tag.Get("db.select"), tagVal) {
					err := Convert(v, vl.Addr().Interface())
					if err != nil {
						return fmt.Errorf("error in field %s: %s", f.As, err.Error())
					}
				}
			}
		*/
	}
	return nil
}