made code more consistent

This commit is contained in:
Karma Riuk
2025-06-23 11:23:28 +02:00
parent 18c1c32a8c
commit 9ddb0fcb7e
3 changed files with 23 additions and 25 deletions

View File

@ -188,8 +188,9 @@
before before
the comment was made.</li> the comment was made.</li>
</ul> </ul>
<pre><code>{ <pre><code>[
"1234": { {
"id": "1234",
"files": { "files": {
"src/Main.java": "public class Main { ... }" "src/Main.java": "public class Main { ... }"
}, },
@ -197,7 +198,7 @@
"src/Main.java": "@@ -1,3 +1,6 @@ ..." "src/Main.java": "@@ -1,3 +1,6 @@ ..."
} }
} }
}</code></pre> ]</code></pre>
</details> </details>
<details> <details>
@ -210,22 +211,21 @@
and and
the exact location of the comment.</li> the exact location of the comment.</li>
</ul> </ul>
<pre><code lang="json">{ <pre><code lang="json">[
"5678": { {
"id": "5678",
"files": { ... }, "files": { ... },
"diffs": { ... }, "diffs": { ... },
"comments": [ "comments": [
{ {
"body": "Consider simplifying this logic.", "body": "Consider simplifying this logic.",
"file": "src/Util.java", "file": "src/Util.java",
"location": { "from_": 42,
"start_line": 42, "to": 45
"end_line": 45
}
} }
] ]
} }
}</code></pre> ]</code></pre>
</details> </details>
</section> </section>
@ -265,21 +265,21 @@
starting line, the ending line (or null), and the comment text itself. starting line, the ending line (or null), and the comment text itself.
</p> </p>
<p> <p>
All fields are required and must follow the expected types exactly. The <code>line_to</code> All fields are required and must follow the expected types exactly. The <code>from_</code>
field can be null if the comment applies to a single line. field can be null if the comment applies to a single line.
</p> </p>
<pre><code> <pre><code>
{ {
"1234": { "1234": {
"path": "src/Main.java", "path": "src/Main.java",
"line_from": 10, "from_": 10,
"line_to": 12, "to": 12,
"body": "Consider extracting this block into a separate method." "body": "Consider extracting this block into a separate method."
}, },
"5678": { "5678": {
"path": "src/Util.java", "path": "src/Util.java",
"line_from": 42, "from_": null,
"line_to": null, "to": 42,
"body": "You might want to add a null check here." "body": "You might want to add a null check here."
} }
} }

View File

@ -51,8 +51,8 @@ class Metadata:
@dataclass @dataclass
class CommentGenSubmission: class CommentGenSubmission:
path: str path: str
line_from: int from_: Optional[int]
line_to: Optional[int] to: int
body: str body: str
@classmethod @classmethod
@ -61,15 +61,13 @@ class CommentGenSubmission:
raise InvalidJsonFormatError("Submitted json doesn't contain an object") raise InvalidJsonFormatError("Submitted json doesn't contain an object")
if not all(k in data and isinstance(data[k], str) for k in ["path", "body"]): if not all(k in data and isinstance(data[k], str) for k in ["path", "body"]):
raise InvalidJsonFormatError("Submitted json doesn't contain the required fields") raise InvalidJsonFormatError("Submitted json doesn't contain the required fields")
if not all( if not all(k in data and isinstance(data[k], (int, type(None))) for k in ["from_", "to"]):
k in data and isinstance(data[k], (int, type(None))) for k in ["line_from", "line_to"]
):
raise InvalidJsonFormatError("Submitted json doesn't contain the required fields") raise InvalidJsonFormatError("Submitted json doesn't contain the required fields")
return cls( return cls(
path=data["path"], path=data["path"],
line_from=data["line_from"], from_=data.get("from_"),
line_to=data.get("line_to"), to=data["to"],
body=data["body"], body=data["body"],
) )

View File

@ -13,7 +13,7 @@ ARCHIVES_ROOT = os.environ["ARCHIVES_ROOT"]
def comment_distance(submission: CommentGenSubmission, entry: Comment): def comment_distance(submission: CommentGenSubmission, entry: Comment):
if entry.from_ is None and entry.to is None: if entry.from_ is None and entry.to is None:
return "NA" return "NA"
if submission.line_from is None and submission.line_to is None: if submission.from_ is None and submission.to is None:
return "NA" return "NA"
# Collapse missing endpoints to the one defined endpoint # Collapse missing endpoints to the one defined endpoint
@ -21,8 +21,8 @@ def comment_distance(submission: CommentGenSubmission, entry: Comment):
start1 = entry.from_ if entry.from_ is not None else entry.to start1 = entry.from_ if entry.from_ is not None else entry.to
end1 = entry.to if entry.to is not None else entry.from_ end1 = entry.to if entry.to is not None else entry.from_
# For submission: # For submission:
start2 = submission.line_from if submission.line_from is not None else submission.line_to start2 = submission.from_ if submission.from_ is not None else submission.to
end2 = submission.line_to if submission.line_to is not None else submission.line_from end2 = submission.to if submission.to is not None else submission.from_
# Now both start1,end1 and start2,end2 are non-None # Now both start1,end1 and start2,end2 are non-None
# Normalize in case from > to (just in case): # Normalize in case from > to (just in case):