Skip to content
This repository was archived by the owner on Jul 28, 2025. It is now read-only.

Commit 9b92eb5

Browse files
committed
circular dependencies removed
1 parent a29f6f3 commit 9b92eb5

File tree

5 files changed

+76
-79
lines changed

5 files changed

+76
-79
lines changed

src/helpers/DataObjectHelper.ts

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
import { LocalSettings } from "cdm/SettingsModel";
21
import { DataObject, Literal } from "obsidian-dataview";
32
import { DataviewService } from "services/DataviewService";
4-
import { LOGGER } from "services/Logger";
5-
import { ParseService } from "services/ParseService";
63

74
/**
85
* @description Method to perform a deep merge of two DataObjects
@@ -51,40 +48,4 @@ export function generateLiteral(nestedKey: string, value: Literal): Literal {
5148
} else {
5249
return { [key]: generateLiteral(keys.join("."), value) };
5350
}
54-
}
55-
56-
/**
57-
* Obtain the value of a nested object in function of the nested key (a.b.c) using recursion
58-
* I.E.:
59-
* nestedKey = "a.b.c"
60-
* object = {a: {b: {c: "test"}}}
61-
* expected result = "test"
62-
* @param nestedKey
63-
* @param original
64-
*/
65-
export function obtainAnidatedLiteral(nestedKey: string, original: Literal, type: string, config: LocalSettings): Literal {
66-
const keys = nestedKey.split(".");
67-
const key = keys.shift();
68-
const wrapped = DataviewService.wrapLiteral(original);
69-
if (wrapped.value === undefined) {
70-
LOGGER.debug(
71-
`nested key ${nestedKey} not found in object ${original}`
72-
);
73-
return null;
74-
}
75-
76-
if (keys.length === 0) {
77-
if (wrapped.type === "object") {
78-
return ParseService.parseLiteral((original as DataObject)[key], type, config);
79-
} else {
80-
return original;
81-
}
82-
} else if (wrapped.type !== "object") {
83-
LOGGER.debug(
84-
`nested key ${nestedKey} not found in object ${original}`
85-
);
86-
return null;
87-
} else {
88-
return obtainAnidatedLiteral(keys.join("."), (original as DataObject)[key], type, config);
89-
}
9051
}

src/helpers/FileManagement.ts

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import HelperException from "errors/HelperException";
44
import { normalizePath, TAbstractFile, TFile, TFolder, Vault } from "obsidian";
55
import { SourceDataTypes } from "helpers/Constants";
66
import { RowDataType } from "cdm/FolderModel";
7-
import { VaultManagerDB } from "services/FileManagerService";
87

98
export function resolve_tfile(file_str: string, restrict = true): TFile {
109
file_str = normalizePath(file_str);
@@ -147,40 +146,6 @@ export const resolveNewFilePath = ({
147146
return `${folderPath}${subfolders ? `/${subfolders}` : ""}`;
148147
};
149148

150-
/**
151-
* Generate a new file with the structure of a database view
152-
* @param folderPath
153-
* @param filename
154-
* @param ddbbConfig
155-
* @returns
156-
*/
157-
export async function create_row_file(
158-
folderPath: string,
159-
filename: string,
160-
ddbbConfig: LocalSettings
161-
): Promise<string> {
162-
let trimedFilename = filename.replace(/\.[^/.]+$/, "").trim();
163-
let filepath = `${folderPath}/${trimedFilename}.md`;
164-
// Validate possible duplicates
165-
let sufixOfDuplicate = 0;
166-
while (resolve_tfile(filepath, false)) {
167-
sufixOfDuplicate++;
168-
filepath = `${folderPath}/${trimedFilename}-${sufixOfDuplicate}.md`;
169-
}
170-
171-
if (sufixOfDuplicate > 0) {
172-
trimedFilename = `${trimedFilename}-${sufixOfDuplicate}`;
173-
filename = `${trimedFilename} copy(${sufixOfDuplicate})`;
174-
}
175-
// Add note to persist row
176-
await VaultManagerDB.create_markdown_file(
177-
resolve_tfolder(folderPath),
178-
trimedFilename,
179-
ddbbConfig
180-
);
181-
return filepath;
182-
}
183-
184149
/**
185150
* Remove all not readable characters of yaml and trim the string
186151
*

src/services/FileManagerService.ts

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { RowDatabaseFields } from "cdm/DatabaseModel";
22
import { NoteContentAction } from "cdm/FolderModel";
33
import { LocalSettings } from "cdm/SettingsModel";
44
import { FileContent } from "helpers/FileContent";
5-
import { resolve_tfile } from "helpers/FileManagement";
5+
import { resolve_tfile, resolve_tfolder } from "helpers/FileManagement";
66
import { Notice, parseYaml, TFile, TFolder } from "obsidian";
77
import { parseFrontmatterFieldsToString, parseInlineFieldsToString } from "parsers/RowDatabaseFieldsToFile";
88
import { LOGGER } from "services/Logger";
@@ -131,6 +131,40 @@ class VaultManager {
131131
}
132132
}
133133

134+
/**
135+
* Generate a new file with the structure of a database view
136+
* @param folderPath
137+
* @param filename
138+
* @param ddbbConfig
139+
* @returns
140+
*/
141+
async create_row_file(
142+
folderPath: string,
143+
filename: string,
144+
ddbbConfig: LocalSettings
145+
): Promise<string> {
146+
let trimedFilename = filename.replace(/\.[^/.]+$/, "").trim();
147+
let filepath = `${folderPath}/${trimedFilename}.md`;
148+
// Validate possible duplicates
149+
let sufixOfDuplicate = 0;
150+
while (resolve_tfile(filepath, false)) {
151+
sufixOfDuplicate++;
152+
filepath = `${folderPath}/${trimedFilename}-${sufixOfDuplicate}.md`;
153+
}
154+
155+
if (sufixOfDuplicate > 0) {
156+
trimedFilename = `${trimedFilename}-${sufixOfDuplicate}`;
157+
filename = `${trimedFilename} copy(${sufixOfDuplicate})`;
158+
}
159+
// Add note to persist row
160+
await this.create_markdown_file(
161+
resolve_tfolder(folderPath),
162+
trimedFilename,
163+
ddbbConfig
164+
);
165+
return filepath;
166+
}
167+
134168
/**
135169
* Singleton instance
136170
* @returns {VaultManager}

src/services/ParseService.ts

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { LOGGER } from "services/Logger";
44
import { DataviewService } from "services/DataviewService";
55
import { LocalSettings } from "cdm/SettingsModel";
66
import { RowDataType, TableColumn } from "cdm/FolderModel";
7-
import { deepMerge, generateLiteral, obtainAnidatedLiteral } from "helpers/DataObjectHelper";
7+
import { deepMerge, generateLiteral } from "helpers/DataObjectHelper";
88
import ParseBuilder from "./parseServiceHelpers/ParseBuilder";
99

1010
class Parse {
@@ -101,11 +101,47 @@ class Parse {
101101
public parseRowToCell(row: RowDataType, column: TableColumn, type: string, config: LocalSettings): Literal {
102102
let literal = row[column.key] as Literal;
103103
if (column.nestedKey && literal !== undefined) {
104-
literal = obtainAnidatedLiteral(column.nestedKey, literal, type, config);
104+
literal = this.obtainAnidatedLiteral(column.nestedKey, literal, type, config);
105105
}
106106
return this.parseLiteral(literal, type, config);
107107
}
108108

109+
/**
110+
* Obtain the value of a nested object in function of the nested key (a.b.c) using recursion
111+
* I.E.:
112+
* nestedKey = "a.b.c"
113+
* object = {a: {b: {c: "test"}}}
114+
* expected result = "test"
115+
* @param nestedKey
116+
* @param original
117+
*/
118+
private obtainAnidatedLiteral(nestedKey: string, original: Literal, type: string, config: LocalSettings): Literal {
119+
const keys = nestedKey.split(".");
120+
const key = keys.shift();
121+
const wrapped = DataviewService.wrapLiteral(original);
122+
if (wrapped.value === undefined) {
123+
LOGGER.debug(
124+
`nested key ${nestedKey} not found in object ${original}`
125+
);
126+
return null;
127+
}
128+
129+
if (keys.length === 0) {
130+
if (wrapped.type === "object") {
131+
return ParseService.parseLiteral((original as DataObject)[key], type, config);
132+
} else {
133+
return original;
134+
}
135+
} else if (wrapped.type !== "object") {
136+
LOGGER.debug(
137+
`nested key ${nestedKey} not found in object ${original}`
138+
);
139+
return null;
140+
} else {
141+
return this.obtainAnidatedLiteral(keys.join("."), (original as DataObject)[key], type, config);
142+
}
143+
}
144+
109145
/**
110146
* Singleton instance
111147
* @returns {VaultManager}

src/stateManagement/data/handlers/AddRowHandlerAction.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { RowDataType, TableColumn } from "cdm/FolderModel";
22
import { LocalSettings } from "cdm/SettingsModel";
33
import { DataState, TableActionResponse } from "cdm/TableStateInterface";
4-
import { create_row_file, destination_folder } from "helpers/FileManagement";
4+
import { destination_folder } from "helpers/FileManagement";
55
import { DateTime } from "luxon";
66
import { Link } from "obsidian-dataview";
7+
import { VaultManagerDB } from "services/FileManagerService";
78
import NoteInfo from "services/NoteInfo";
89
import { AbstractTableAction } from "stateManagement/AbstractTableAction";
910

@@ -12,7 +13,7 @@ export default class AddRowlHandlerAction extends AbstractTableAction<DataState>
1213
const { view, set, implementation } = tableActionResponse;
1314
implementation.actions.addRow = async (filename: string, columns: TableColumn[], ddbbConfig: LocalSettings) => {
1415
const folderPath = destination_folder(view, ddbbConfig);
15-
const filepath = await create_row_file(folderPath, filename, ddbbConfig);
16+
const filepath = await VaultManagerDB.create_row_file(folderPath, filename, ddbbConfig);
1617

1718
const newNote = new NoteInfo({
1819
file: {

0 commit comments

Comments
 (0)