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
the comment was made.</li>
</ul>
<pre><code>{
"1234": {
<pre><code>[
{
"id": "1234",
"files": {
"src/Main.java": "public class Main { ... }"
},
@ -197,7 +198,7 @@
"src/Main.java": "@@ -1,3 +1,6 @@ ..."
}
}
}</code></pre>
]</code></pre>
</details>
<details>
@ -210,22 +211,21 @@
and
the exact location of the comment.</li>
</ul>
<pre><code lang="json">{
"5678": {
<pre><code lang="json">[
{
"id": "5678",
"files": { ... },
"diffs": { ... },
"comments": [
{
"body": "Consider simplifying this logic.",
"file": "src/Util.java",
"location": {
"start_line": 42,
"end_line": 45
}
"from_": 42,
"to": 45
}
]
}
}</code></pre>
]</code></pre>
</details>
</section>
@ -265,21 +265,21 @@
starting line, the ending line (or null), and the comment text itself.
</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.
</p>
<pre><code>
{
"1234": {
"path": "src/Main.java",
"line_from": 10,
"line_to": 12,
"from_": 10,
"to": 12,
"body": "Consider extracting this block into a separate method."
},
"5678": {
"path": "src/Util.java",
"line_from": 42,
"line_to": null,
"from_": null,
"to": 42,
"body": "You might want to add a null check here."
}
}

View File

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

View File

@ -13,7 +13,7 @@ ARCHIVES_ROOT = os.environ["ARCHIVES_ROOT"]
def comment_distance(submission: CommentGenSubmission, entry: Comment):
if entry.from_ is None and entry.to is None:
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"
# 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
end1 = entry.to if entry.to is not None else entry.from_
# For submission:
start2 = submission.line_from if submission.line_from is not None else submission.line_to
end2 = submission.line_to if submission.line_to is not None else submission.line_from
start2 = submission.from_ if submission.from_ is not None else submission.to
end2 = submission.to if submission.to is not None else submission.from_
# Now both start1,end1 and start2,end2 are non-None
# Normalize in case from > to (just in case):