コード例 #1
0
ファイル: imply.go プロジェクト: logtcn/TMSU
func addImplications(store *storage.Storage, tx *storage.Tx, tagArgs []string) (error, warnings) {
	log.Infof(2, "loading settings")

	settings, err := store.Settings(tx)
	if err != nil {
		return err, nil
	}

	implyingTagArg := tagArgs[0]
	impliedTagArgs := tagArgs[1:]

	implyingTagName, implyingValueName := parseTagEqValueName(implyingTagArg)

	implyingTag, err := store.TagByName(tx, implyingTagName)
	if err != nil {
		return err, nil
	}
	if implyingTag == nil {
		if settings.AutoCreateTags() {
			implyingTag, err = createTag(store, tx, implyingTagName)
			if err != nil {
				return err, nil
			}
		} else {
			return NoSuchTagError{implyingTagName}, nil
		}
	}

	implyingValue, err := store.ValueByName(tx, implyingValueName)
	if err != nil {
		return err, nil
	}
	if implyingValue == nil {
		if settings.AutoCreateValues() {
			implyingValue, err = createValue(store, tx, implyingValueName)
			if err != nil {
				return err, nil
			}
		} else {
			return NoSuchValueError{implyingValueName}, nil
		}
	}

	warnings := make(warnings, 0, 10)
	for _, impliedTagArg := range impliedTagArgs {
		impliedTagName, impliedValueName := parseTagEqValueName(impliedTagArg)

		impliedTag, err := store.TagByName(tx, impliedTagName)
		if err != nil {
			return err, warnings
		}
		if impliedTag == nil {
			if settings.AutoCreateTags() {
				impliedTag, err = createTag(store, tx, impliedTagName)
				if err != nil {
					return err, warnings
				}
			} else {
				warnings = append(warnings, fmt.Sprintf("no such tag '%v'", impliedTagName))
				continue
			}
		}

		impliedValue, err := store.ValueByName(tx, impliedValueName)
		if err != nil {
			return err, warnings
		}
		if impliedValue == nil {
			if settings.AutoCreateValues() {
				impliedValue, err = createValue(store, tx, impliedValueName)
				if err != nil {
					return err, warnings
				}
			} else {
				warnings = append(warnings, fmt.Sprintf("no such value '%v'", impliedValueName))
				continue
			}
		}

		log.Infof(2, "adding tag implication of '%v' to '%v'", implyingTagArg, impliedTagArg)

		if err = store.AddImplication(tx, entities.TagIdValueIdPair{implyingTag.Id, implyingValue.Id}, entities.TagIdValueIdPair{impliedTag.Id, impliedValue.Id}); err != nil {
			return fmt.Errorf("cannot add implication of '%v' to '%v': %v", implyingTagArg, impliedTagArg, err), warnings
		}
	}

	return nil, warnings
}