support field aliases v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Wed, 16 Jan 2019 17:48:06 +0100
branchv_0
changeset 5 ec661baf433a
parent 4 d44ed75822e7
child 6 35607c973cf5
support field aliases - aliases can be used for renames and also for duplication - some specific fields might even fill multiple attributes with different values - example of rename and duplication: c.fields.push_back(RequestedField(RequestedField::GROUP_FILE, FileAttributeFinder::FIELD_TYPE,{L"type_a", L"type_b"})); // CLIParser.h
src/FileAttributeFinder.h
src/RequestedField.h
src/XattrAttributeFinder.h
--- a/src/FileAttributeFinder.h	Wed Jan 16 17:23:05 2019 +0100
+++ b/src/FileAttributeFinder.h	Wed Jan 16 17:48:06 2019 +0100
@@ -82,8 +82,12 @@
 
 	virtual vector<AttributeMetadata> toMetadata(const RequestedField& field) override {
 		if (field.group == RequestedField::GROUP_FILE) {
-			if (field.name == FIELD_SIZE) return { AttributeMetadata{field.name, TypeId::INTEGER}};
-			else return { AttributeMetadata{field.name, TypeId::STRING}};
+			vector<AttributeMetadata> metadata;
+			for (string_t alias : field.getAliases()) {
+				if (field.name == FIELD_SIZE) metadata.push_back(AttributeMetadata{alias, TypeId::INTEGER});
+				else metadata.push_back(AttributeMetadata{alias, TypeId::STRING});
+			}
+			return metadata;
 		} else {
 			return {};
 		}
@@ -100,28 +104,30 @@
 
 	virtual void writeField(RelationalWriter* writer, const RequestedField& field) override {
 		if (field.group == RequestedField::GROUP_FILE) {
-			if (field.name == FIELD_NAME) {
-				writer->writeAttribute(currentFile.filename().wstring());
-			} else if (field.name == FIELD_PATH_ORIGINAL) {
-				writer->writeAttribute(currentFile.wstring());
-			} else if (field.name == FIELD_PATH_ABSOLUTE) {
-				writer->writeAttribute(fs::absolute(currentFile).wstring());
-			} else if (field.name == FIELD_PATH_CANONICAL) {
-				writer->writeAttribute(fs::canonical(currentFile).wstring());
-			} else if (field.name == FIELD_TYPE) {
-				writer->writeAttribute(getType(currentFile));
-			} else if (field.name == FIELD_SIZE) {
-				integer_t size = fs::is_regular_file(currentFile) ? fs::file_size(currentFile) : 0;
-				writer->writeAttribute(&size, typeid (size));
-			} else if (field.name == FIELD_OWNER) {
-				if (currentOwner.empty()) fetchOwner(currentFile, currentOwner, currentGroup);
-				writer->writeAttribute(currentOwner);
-			} else if (field.name == FIELD_GROUP) {
-				if (currentOwner.empty()) fetchOwner(currentFile, currentOwner, currentGroup);
-				writer->writeAttribute(currentGroup);
-			} else {
-				// TODO: should not happend; check supported attributes in toMetadata()?
-				writer->writeAttribute(L"");
+			for (string_t alias : field.getAliases()) {
+				if (field.name == FIELD_NAME) {
+					writer->writeAttribute(currentFile.filename().wstring());
+				} else if (field.name == FIELD_PATH_ORIGINAL) {
+					writer->writeAttribute(currentFile.wstring());
+				} else if (field.name == FIELD_PATH_ABSOLUTE) {
+					writer->writeAttribute(fs::absolute(currentFile).wstring());
+				} else if (field.name == FIELD_PATH_CANONICAL) {
+					writer->writeAttribute(fs::canonical(currentFile).wstring());
+				} else if (field.name == FIELD_TYPE) {
+					writer->writeAttribute(getType(currentFile));
+				} else if (field.name == FIELD_SIZE) {
+					integer_t size = fs::is_regular_file(currentFile) ? fs::file_size(currentFile) : 0;
+					writer->writeAttribute(&size, typeid (size));
+				} else if (field.name == FIELD_OWNER) {
+					if (currentOwner.empty()) fetchOwner(currentFile, currentOwner, currentGroup);
+					writer->writeAttribute(currentOwner);
+				} else if (field.name == FIELD_GROUP) {
+					if (currentOwner.empty()) fetchOwner(currentFile, currentOwner, currentGroup);
+					writer->writeAttribute(currentGroup);
+				} else {
+					// TODO: should not happend; check supported attributes in toMetadata()?
+					writer->writeAttribute(L"");
+				}
 			}
 		}
 	}
--- a/src/RequestedField.h	Wed Jan 16 17:23:05 2019 +0100
+++ b/src/RequestedField.h	Wed Jan 16 17:48:06 2019 +0100
@@ -39,6 +39,14 @@
 	RequestedField(const string_t& group, const string_t& name, const std::vector<string_t>& aliases = {}, const std::map<string_t, string_t>& options = {}) : group(group), name(name), aliases(aliases), options(options) {
 	}
 
+	/**
+	 * @return aliases or vector with single item (name), if aliases are empty
+	 */
+	std::vector<string_t> getAliases() const {
+		if (aliases.empty()) return {name};
+		else return aliases;
+	}
+
 	virtual ~RequestedField() {
 	}
 };
--- a/src/XattrAttributeFinder.h	Wed Jan 16 17:23:05 2019 +0100
+++ b/src/XattrAttributeFinder.h	Wed Jan 16 17:48:06 2019 +0100
@@ -56,8 +56,13 @@
 public:
 
 	virtual vector<AttributeMetadata> toMetadata(const RequestedField& field) override {
-		if (field.group == RequestedField::GROUP_XATTR) return { AttributeMetadata{field.name, TypeId::STRING}};
-		else return {};
+		if (field.group == RequestedField::GROUP_XATTR) {
+			vector<AttributeMetadata> metadata;
+			for (string_t alias : field.getAliases()) metadata.push_back(AttributeMetadata{alias, TypeId::STRING});
+			return metadata;
+		} else {
+			return {};
+		}
 	}
 
 	void startFile(const fs::path& file) override {
@@ -68,7 +73,9 @@
 	};
 
 	virtual void writeField(RelationalWriter* writer, const RequestedField& field) override {
-		if (field.group == RequestedField::GROUP_XATTR) writer->writeAttribute(getXattr(currentFile, field.name));
+		for (string_t alias : field.getAliases()) {
+			if (field.group == RequestedField::GROUP_XATTR) writer->writeAttribute(getXattr(currentFile, field.name));
+		}
 	}
 
 	virtual ~XattrAttributeFinder() override {