Repository navigation
v0.3.0 #2
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
Open
sachin-panayil
wants to merge
4
commits into
main
Choose a base branch
from
sachin/deep-merge
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
v0.3.0 #2
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,21 +8,87 @@ export interface AssembleOptions { | |
| now?: () => Date; | ||
| } | ||
|
|
||
| // fields whose final value needs selection/synthesis logic, not a plain override. | ||
| // everything a caller sends that IS a plain override just flows through the spread below and does NOT belong here. | ||
| interface DerivedView { | ||
| repositoryURL?: string; | ||
| feedbackMechanism?: string; | ||
| SBOM?: string; | ||
| description?: string; | ||
| tags?: string[]; | ||
| status?: string; | ||
| reuseFrequency?: { forks?: number; clones?: number }; | ||
| date?: { created?: string; lastModified?: string; metadataLastUpdated?: string }; | ||
| type Policy = "existing" | "observed" | "union"; | ||
|
|
||
| // who wins when both sides have a value. anything unlisted is "existing" | ||
| const POLICY: Record<string, Policy> = { | ||
| repositoryURL: "observed", | ||
| repositoryVisibility: "observed", | ||
| laborHours: "observed", | ||
| "reuseFrequency.forks": "observed", | ||
| "date.created": "observed", | ||
| "date.lastModified": "observed", | ||
| tags: "union", | ||
| reusedCode: "union", | ||
| }; | ||
|
Comment on lines
+13
to
+23
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Love this, this is a very clever and understandable way of codifying merge logic 🔥 |
||
|
|
||
| const isPlainObject = (value: unknown): value is Record<string, unknown> => | ||
| typeof value === "object" && value !== null && !Array.isArray(value); | ||
|
|
||
| // a value still sitting at its baseline default counts as unset, so detection can fill it | ||
| const isUnset = (value: unknown, base: unknown): boolean => | ||
| value === undefined || | ||
| value === null || | ||
| JSON.stringify(value) === JSON.stringify(base); | ||
|
|
||
| const sameText = (a: unknown, b: unknown): boolean => | ||
| typeof a === "string" && | ||
| typeof b === "string" && | ||
| a !== "" && | ||
| a.toLowerCase() === b.toLowerCase(); | ||
|
|
||
| // objects like reusedCode entries match on URL or name | ||
| const sameItem = (a: unknown, b: unknown): boolean => | ||
| isPlainObject(a) && isPlainObject(b) | ||
| ? sameText(a.URL, b.URL) || sameText(a.name, b.name) | ||
| : a === b; | ||
|
|
||
| function union(existing: unknown, observed: unknown): unknown[] { | ||
| const result = Array.isArray(existing) ? [...existing] : []; | ||
| for (const item of Array.isArray(observed) ? observed : []) { | ||
| if (!result.some((kept) => sameItem(kept, item))) result.push(item); | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| function mergeValue( | ||
| path: string, | ||
| base: unknown, | ||
| existing: unknown, | ||
| observed: unknown, | ||
| ): unknown { | ||
| const objects = [base, existing, observed].filter(isPlainObject); | ||
|
|
||
| if (objects.length > 0) { | ||
| const child = (value: unknown, key: string) => | ||
| isPlainObject(value) ? value[key] : undefined; | ||
| const result: Record<string, unknown> = {}; | ||
|
|
||
| for (const key of new Set(objects.flatMap(Object.keys))) { | ||
| const value = mergeValue( | ||
| path ? `${path}.${key}` : key, | ||
| child(base, key), | ||
| child(existing, key), | ||
| child(observed, key), | ||
| ); | ||
| if (value !== undefined) result[key] = value; | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| const policy = POLICY[path] ?? "existing"; | ||
| if (policy === "union") return union(existing, observed); | ||
|
|
||
| const [first, second] = | ||
| policy === "existing" ? [existing, observed] : [observed, existing]; | ||
| if (!isUnset(first, base)) return first; | ||
| if (!isUnset(second, base)) return second; | ||
| return base; | ||
| } | ||
|
|
||
| // merge everything into one code.json. never throws meaning the result may be an incomplete draft. | ||
| // baseline -> cleaned existing file -> freshly observed -> derived (later wins) | ||
| // manual values in the existing file win unless POLICY says the field is observed or unioned. | ||
| // this is meant to be a pure function with no i/o | ||
| export function mergeWith<T extends Record<string, unknown>>( | ||
| baseline: Partial<T>, | ||
|
|
@@ -39,60 +105,37 @@ export function mergeWith<T extends Record<string, unknown>>( | |
| ) | ||
| : {}; | ||
|
|
||
| // step 2: compute the derived fields. | ||
| const obs = observed as unknown as DerivedView; | ||
| const ex = (existing ?? {}) as unknown as DerivedView; | ||
|
|
||
| const repoURL = obs.repositoryURL ?? ex.repositoryURL ?? ""; | ||
|
|
||
| const feedbackMechanism = ex.feedbackMechanism | ||
| ? ex.feedbackMechanism | ||
| : `${repoURL}/issues`; | ||
|
|
||
| const SBOM = ex.SBOM ? ex.SBOM : `${repoURL}/network/dependencies`; | ||
|
|
||
| const description = | ||
| obs.description && obs.description.trim() !== "" | ||
| ? obs.description | ||
| : (ex.description ?? ""); | ||
|
|
||
| const baseTags = obs.tags && obs.tags.length > 0 ? obs.tags : (ex.tags ?? []); | ||
| const tags = | ||
| isArchived && !baseTags.includes("archived") | ||
| ? [...baseTags, "archived"] | ||
| : baseTags; | ||
|
|
||
| const reuseFrequency = { | ||
| forks: obs.reuseFrequency?.forks ?? ex.reuseFrequency?.forks ?? 0, | ||
| clones: ex.reuseFrequency?.clones ?? 0, | ||
| }; | ||
|
|
||
| const date = { | ||
| created: obs.date?.created ?? ex.date?.created ?? "", | ||
| lastModified: obs.date?.lastModified ?? ex.date?.lastModified ?? "", | ||
| // observed keys the variant doesn't define are dropped silently | ||
| const cleanedObserved = filterValidFields( | ||
| baseline, | ||
| observed as Record<string, unknown>, | ||
| ); | ||
|
|
||
| // step 2: merge field by field. | ||
| const result = mergeValue( | ||
| "", | ||
| baseline, | ||
| cleanedExisting, | ||
| cleanedObserved, | ||
| ) as Record<string, unknown>; | ||
|
|
||
| // step 3: fill in what can be computed from the merged result. | ||
| const repoURL = result.repositoryURL ?? ""; | ||
| if (!result.feedbackMechanism) result.feedbackMechanism = `${repoURL}/issues`; | ||
| if (!result.SBOM) result.SBOM = `${repoURL}/network/dependencies`; | ||
|
|
||
| result.date = { | ||
| ...(isPlainObject(result.date) ? result.date : {}), | ||
| metadataLastUpdated: (now?.() ?? new Date()).toISOString(), | ||
| }; | ||
|
|
||
| const derived: Record<string, unknown> = { | ||
| feedbackMechanism, | ||
| SBOM, | ||
| description, | ||
| tags, | ||
| reuseFrequency, | ||
| date, | ||
| }; | ||
|
|
||
| if (isArchived) derived.status = "Archival"; | ||
|
|
||
| // step 3: merge with precedence (later wins). | ||
| const result = { | ||
| ...baseline, | ||
| ...cleanedExisting, | ||
| ...observed, | ||
| ...derived, | ||
| }; | ||
| if (isArchived) { | ||
| const tags = Array.isArray(result.tags) ? result.tags : []; | ||
| result.status = "Archival"; | ||
| result.tags = tags.includes("archived") ? tags : [...tags, "archived"]; | ||
| } | ||
|
|
||
| return result as unknown as T; | ||
| return result as T; | ||
| } | ||
|
|
||
| // merge, then require the result to be a valid, finished code.json. | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| import { neutralProfile } from "./neutral.js"; | ||
| import { cmsProfile } from "./cms.js"; | ||
|
|
||
| export const profiles = { neutral: neutralProfile, cms: cmsProfile } as const; | ||
| export type ProfileName = keyof typeof profiles; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did a review of all the code.json fields and I agree with the rule classifications you made here 🙌