-
Notifications
You must be signed in to change notification settings - Fork 443
Detect changes in json files regarding dictionary types #38
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -531,8 +531,26 @@ let EmitDictionaries flavor = | |
| "Object" -> Pt.printl "interface %s {" dict.Name | ||
| _ -> Pt.printl "interface %s extends %s {" dict.Name dict.Extends | ||
|
||
let emitJsonProperty (p: ItemsType.Root) = | ||
Pt.printl "%s: %s;" p.Name.Value p.Type.Value | ||
|
||
let removedPropNames = | ||
getRemovedItems ItemKind.Property flavor | ||
|> Array.filter (matchInterface dict.Name) | ||
|> Array.map (fun rp -> rp.Name.Value) | ||
|> Set.ofArray | ||
let addedProps = | ||
getAddedItems ItemKind.Property flavor | ||
|> Array.filter (matchInterface dict.Name) | ||
|
||
Pt.increaseIndent() | ||
dict.Members |> Array.iter (fun m -> Pt.printl "%s?: %s;" m.Name (DomTypeToTsType m.Type)) | ||
Array.iter emitJsonProperty addedProps | ||
dict.Members | ||
|> Array.filter (fun m -> not (Set.contains m.Name removedPropNames)) | ||
|> Array.iter (fun m -> | ||
match (findOverriddenItem m.Name ItemKind.Property dict.Name) with | ||
| Some om -> emitJsonProperty om | ||
| None -> Pt.printl "%s?: %s;" m.Name (DomTypeToTsType m.Type)) | ||
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. there's always multiple way to do things in F#. My personal style preference would be to do let isRemovedPropName =
let removedPropNames = ...
not << fun m -> Set.contains m.Name removedPropNames
(* ... find and emit addedProps ... *)
for m in Array.filter isRemovedPropName dict.Members do
(* ... same match body as before ... *) This makes the side-effecting syntactically obvious and reduces the overhead from F#'s lambda syntax. 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. It is more or less subjective when it comes to the syntax preferences though. I used the continuation style because it makes the data flow clearer, and avoids the burden of naming things. Though I agree both styles have merits. |
||
Pt.decreaseIndent() | ||
Pt.printl "}" | ||
Pt.printl "" | ||
|
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.
I would tack on
|> Set.ofArray
to make the contains check below faster
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.
Which contains check?
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.
I would change
Array.contains
toSet.contains
.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.
How is
Set.contains
faster thanArray.contains
? My impression is the opposite, because at leastSet.contains
has an implicit conversion fromArray
toSet
.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.
Set.contains
doesn't do any implicit conversions. You'd pay the construction cost once when building removedPropNames by adding|> Set.ofArray
.Generally, Set.contains is O(lg n) or O(1) and Array.contains is O(n), so if removedPropNames is ever long-ish (10-100 or more items probably?) it will be worth it.
However, I can't find any documentation on
Array.contains
! Where doesArray.contains
come from? I see that Shared.fs addsSeq.contains x = Seq.exists ((=)x)
, which is O(n), but I can't find a definition ofArray.contains
in our own code.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.
Good point. For some reason I was thinking about
Seq
instead ofSet
, therefore had the impression thatArray.contains
is faster. Will update soon.I believe
Array.contains
is added in F# 4.0 which was not very well documented.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.
Done.