Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split simplifyClaim and simplifySnak #122

Merged
merged 13 commits into from
Mar 3, 2024

Conversation

maxlath
Copy link
Owner

@maxlath maxlath commented Jun 15, 2023

simplifyClaim and simplifySnak are currently aliases of the same function, which creates complexity and confusion, as a claim has itself a mainsnak. By splitting those functions, typing also becomes easier.

Any opinion @EdJoPaTo @mshd?

Copy link
Contributor

@EdJoPaTo EdJoPaTo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really like the approach to reduce complexity and confusion which also simplifies the typings!

I have a bunch of suggestions. For now I haven't continued looking more into it so there might be more things when the current batch of suggestion is handled.

Thank you for asking for feedback on it!

src/helpers/simplify_claims.ts Outdated Show resolved Hide resolved
@@ -98,7 +98,7 @@ export const parsers = {
'wikibase-sense': entity,
} as const

export function parseClaim (datatype, datavalue, options, claimId) {
export function parseSnak (datatype, datavalue, options) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we provide typings here in an easy way? There are DataType (which needs refactoring in the future but still) and SnakValue.

Not sure what the correct options type would be? But better explicitly any than nothing. Also the = {} is probably missing currently?

Suggested change
export function parseSnak (datatype, datavalue, options) {
export function parseSnak (datatype: DataType, datavalue: SnakValue, options: any = {}) {

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would need more work due to the hacks on datatype this function does:

  // Known case of missing datatype: form.claims, sense.claims
  datatype = datatype || datavalue.type
  // Known case requiring this: legacy "muscial notation" datatype
  datatype = datatype.replace(' ', '-')

I would let that for another PR ^^

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, this seems to work a01f5ab, but hacky ><

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Further addressed in 0805fee

src/helpers/simplify_claims.ts Show resolved Hide resolved
src/helpers/simplify_claims.ts Show resolved Hide resolved
src/helpers/simplify_claims.ts Outdated Show resolved Hide resolved
src/helpers/simplify_claims.ts Outdated Show resolved Hide resolved
Comment on lines 57 to 61
let valueObj
if (isPlainObject(value)) {
valueObj = value as CustomSimplifiedClaim
} else {
valueObj = { value } as CustomSimplifiedClaim
}
Copy link
Contributor

@EdJoPaTo EdJoPaTo Jun 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be like the following suggestion but that's currently not possible as it results in a bunch of type errors…

-  let valueObj
-  if (isPlainObject(value)) {
-    valueObj = value as CustomSimplifiedClaim
-  } else {
-    valueObj = { value } as CustomSimplifiedClaim
-  }
+  let valueObj: CustomSimplifiedClaim
+  if (isPlainObject(value)) {
+    valueObj = value
+  } else {
+    valueObj = { value }
+  }

For now I would suggest to just type it explicitly as any so it's noticed easier later on and can be fixed on its own rather than in this PR. (When DataType is refactored it should solve itself then)

Suggested change
let valueObj
if (isPlainObject(value)) {
valueObj = value as CustomSimplifiedClaim
} else {
valueObj = { value } as CustomSimplifiedClaim
}
let valueObj: any // TODO: type as CustomSimplifiedClaim
if (isPlainObject(value)) {
valueObj = value
} else {
valueObj = { value }
}

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addressed in 9755da5

@@ -98,7 +98,7 @@ export const parsers = {
'wikibase-sense': entity,
} as const

export function parseClaim (datatype, datavalue, options, claimId) {
export function parseSnak (datatype, datavalue, options) {
// Known case of missing datatype: form.claims, sense.claims
datatype = datatype || datavalue.type
// Known case requiring this: legacy "muscial notation" datatype
Copy link
Contributor

@EdJoPaTo EdJoPaTo Jun 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When explicitly specifying datatype: DataType this is an expected TypeScript error but we know it works so just ignore it.

Suggested change
// Known case requiring this: legacy "muscial notation" datatype
// @ts-expect-error Known case requiring this: legacy "musical notation" datatype

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't find how to add @ts-expect-error, it tells me that it's an unused directive

@maxlath maxlath force-pushed the split-simplify-claims-and-simplify-snaks branch from f3f8f24 to 0805fee Compare July 31, 2023 14:03
src/types/simplify_claims.ts Outdated Show resolved Hide resolved
src/types/claim.ts Outdated Show resolved Hide resolved
src/helpers/parse_snak.ts Outdated Show resolved Hide resolved
maxlath added a commit that referenced this pull request Aug 1, 2023
@maxlath maxlath force-pushed the split-simplify-claims-and-simplify-snaks branch from f8c5ed1 to 898722e Compare August 1, 2023 12:00
maxlath added a commit that referenced this pull request Aug 1, 2023
Copy link
Contributor

@EdJoPaTo EdJoPaTo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still like how the simplification reduces the complexity and therefore also the typing situation!

src/helpers/parse_snak.ts Outdated Show resolved Hide resolved
src/helpers/parse_snak.ts Outdated Show resolved Hide resolved
maxlath added a commit that referenced this pull request Dec 17, 2023
maxlath added a commit that referenced this pull request Dec 17, 2023
@maxlath maxlath force-pushed the split-simplify-claims-and-simplify-snaks branch from bbc2d82 to d77cbd8 Compare December 17, 2023 11:07
src/helpers/parse_snak.ts Outdated Show resolved Hide resolved
maxlath and others added 13 commits March 3, 2024 22:01
Now simplifySnak is applied to the claim mainsnak, but other claim-specific operations
are performed by simplifyClaim alone
Co-authored-by: EdJoPaTo <rfc-conform-git-commit-email@funny-long-domain-label-everyone-hates-as-it-is-too-long.edjopato.de>
… warning

to prevent a breaking change, even if simplifyReferenceRecord was undocumented
Co-authored-by: EdJoPaTo <rfc-conform-git-commit-email@funny-long-domain-label-everyone-hates-as-it-is-too-long.edjopato.de>
@maxlath maxlath force-pushed the split-simplify-claims-and-simplify-snaks branch from d77cbd8 to 740a737 Compare March 3, 2024 21:01
@maxlath maxlath merged commit 2aeed2c into main Mar 3, 2024
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants