Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

feat: Switch packages for typescript generation code#1196

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Emyrk merged 18 commits intomainfromstevenmasley/redo_typescript_gen
Apr 28, 2022

Conversation

Emyrk
Copy link
Member

@EmyrkEmyrk commentedApr 27, 2022
edited
Loading

Redo typescript autogen

Ok, this got a lot more complicated, but it handles all the cases. I didn't add a unit test, because we will just use the generated types as our testing. If a type is wrong, hopefully the FE team will complain and we fix it.

  • Supports Go types
    • Basics (string/int/etc)
    • Maps
    • Slices
    • Enums
    • Pointers
    • External Types (usesany atm)
      • Some custom external types are hardcoded in (eg: time.Time)

Features

Ignore types

http://github.com/coder/coder/blob/071026deca83f558f9c5be3c70f90cda0105aa82/codersdk/client.go#L29-L29

Choose typescript values

typeExamplestruct {FieldUseStringCustomType`json:"field_string" typescript:"string"`Ignore*http.Client`json:"-" typescript:"-"`}

Issues

  • How do we handletime.Time?

@EmyrkEmyrk marked this pull request as ready for reviewApril 28, 2022 14:46
@EmyrkEmyrk requested a review froma team as acode ownerApril 28, 2022 14:46
@codecov
Copy link

codecovbot commentedApr 28, 2022
edited
Loading

Codecov Report

Merging#1196 (0d37eeb) intomain (816441e) willdecrease coverage by9.36%.
The diff coverage is0.00%.

@@            Coverage Diff             @@##             main    #1196      +/-   ##==========================================- Coverage   66.00%   56.64%   -9.37%==========================================  Files         265      265                Lines       16751    16937     +186       Branches      157      157              ==========================================- Hits        11057     9594    -1463- Misses       4533     6280    +1747+ Partials     1161     1063      -98
FlagCoverage Δ
unittest-go-macos-latest52.88% <0.00%> (-0.57%)⬇️
unittest-go-postgres-?
unittest-go-ubuntu-latest55.37% <0.00%> (-0.65%)⬇️
unittest-go-windows-202252.46% <0.00%> (-0.60%)⬇️
unittest-js66.50% <ø> (ø)
Impacted FilesCoverage Δ
codersdk/client.go59.74% <ø> (ø)
scripts/apitypings/main.go0.00% <0.00%> (ø)
coderd/database/queries.sql.go0.00% <0.00%> (-81.13%)⬇️
coderd/devtunnel/tunnel.go0.00% <0.00%> (-79.67%)⬇️
coderd/database/pubsub.go0.00% <0.00%> (-77.78%)⬇️
coderd/database/db.go0.00% <0.00%> (-55.18%)⬇️
coderd/database/migrate.go0.00% <0.00%> (-45.00%)⬇️
peerbroker/dial.go77.04% <0.00%> (-6.56%)⬇️
coderd/coderdtest/coderdtest.go93.83% <0.00%> (-5.05%)⬇️
cli/server.go54.97% <0.00%> (-3.94%)⬇️
... and8 more

Continue to review full report at Codecov.

Legend -Click here to learn more
Δ = absolute <relative> (impact),ø = not affected,? = missing data
Powered byCodecov. Last update816441e...0d37eeb. Read thecomment docs.

@@ -0,0 +1,39 @@
# APITypings
Copy link
Contributor

Choose a reason for hiding this comment

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

praise: Wonderful documentation!

readonly email: string
readonly username: string
readonly password: string
readonly organization_id: string
Copy link
Contributor

Choose a reason for hiding this comment

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

👍 nice, there it is

Copy link
Contributor

Choose a reason for hiding this comment

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

cc@presleyp 🎉

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

Pretty much any non-primitive (basic type) wasn't included before.uuid is anamedarray type ofbyte.

All now handled. The unhandled cases in the past omitted the field, now they useany and leave a comment.

// Named type "github.com/coder/coder/coderd/database.ParameterValue" unknown, using "any"
// eslint-disable-next-line @typescript-eslint/no-explicit-any
readonlyParameterValue:any

I also comment where I use some "intelligence" about external types.

// This is likely an enum in an external package ("github.com/coder/coder/coderd/database.ParameterSourceScheme")
readonlydefault_source_scheme:string

One thing to note is nothing here is set in stone, and if the FE team thinks a type is wrong, we can fix it. The current implementation comments all guesses though, so you know where things are maybe a bit ambiguous, or could be improved.

Copy link
Contributor

@f0sself0ssel left a comment

Choose a reason for hiding this comment

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

Overall great improvement. Glad we can even add doc comments for types and fields in the future with this route. Only real unknowns are the byte arrays but we can figure that out.

case bs.Info()&types.IsBoolean > 0:
return TypescriptType{ValueType: "boolean"}, nil
case bs.Kind() == types.Byte:
// TODO: @emyrk What is a byte for typescript? A string? A uint8?
Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

If we add[]byte to a type, we can adjust as needed. For now, I just use anumber for a byte, andstring for[]byte 🤷.

TIL about that

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

I was guessing these types should accept the golang marshal json. So this will at leastwork in the sense the 2 types can be communicated over json.

)

// TODO: Handle httpapi.Response and other types
Copy link
Contributor

Choose a reason for hiding this comment

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

We should focus on making this script work perfectly for what we already plan to do incodersdk. If this is relevant with that in mind, you should probably make this more specific to the code that handles this.

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

I don't know if we need it. This is really to match Go types, but the general error/response type is not bad to implement once in TS forall endpoints that use it.

I don't want to run this code on another package, and type inferencing across packages is only possible to an extent.


I removed the TODO, but the basic thing is that external types are not currently handled. The only way to handle them better, is to run this code on those packages as well. Obviously then we want to allowlist types, vs the current deny list approach.

Here is the example of external types:
http://github.com/coder/coder/blob/3865f36c4bf8c2ddfc1c9081cf7bbf1a881675f6/codersdk/templateversions.go#L27-L30

They use database types that include enums. I would like to have a conversation about how to handle these, as we can handle them. Just a matter of "how"

return TypescriptType{ValueType: bs.Name()}, nil
}
case *types.Struct:
// This handles anonymous structs. This should never happen really.
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe we just error and tell the user not to do that? I could go either way, but idk if it's worth explicitly supporting.

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

I like my current approach of just leaving a comment that it should be fixed and acceptany.

I was afraid to error, as it's kinda a pain when the error blocks your work. Theany type is valid typescript, so it stillwill work from a compile time perspective.


I do see the benefit of an error though as it does force the change up front. I just see this code as more a way for the FE and BE to agree on api types, rather than a strict document that has no flexibility.

Copy link
MemberAuthor

@EmyrkEmyrkApr 28, 2022
edited
Loading

Choose a reason for hiding this comment

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

I can cave on this if you feel strongly otherwise. In a month or two, I'm happy to make this more strict as we know more what we want.

// When type checking here, just use the string. You can cast it
// to a types.Basic and get the kind if you want too :shrug:
case arr.Elem().String() == "byte":
// All byte arrays are strings on the typescript.
Copy link
Contributor

Choose a reason for hiding this comment

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

@vapurrmaid could you weigh in here?

Copy link
MemberAuthor

@EmyrkEmyrkApr 28, 2022
edited
Loading

Choose a reason for hiding this comment

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

Yea, really curious here. The json marshal is a string, which is why I chose it. It's base64 encoded. Not ideal still?

https://go.dev/play/p/mv6bH0b9g4N

readonly name: string
// This is likely an enum in an external package ("github.com/coder/coder/coderd/database.ParameterSourceScheme")
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice stop gap 👍

Emyrk reacted with thumbs up emoji
// From codersdk/provisionerdaemons.go:26:6.
export type ProvisionerJobStatus = "pending" | "running" | "succeeded" | "canceling" | "canceled" | "failed"
// From codersdk/provisionerdaemons.go:26:6
export type ProvisionerJobStatus = "canceled" | "canceling" | "failed" | "pending" | "running" | "succeeded"
Copy link
Contributor

Choose a reason for hiding this comment

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

ty for sorting, will prevent flakes

Emyrk reacted with heart emoji
@f0ssel
Copy link
Contributor

If we don't see us returning byte arrays from codersdk in the near future we can leave what is here and kick that can down the road. My guess would be an array of ints but idk for sure.

Copy link
Contributor

@presleyppresleyp left a comment

Choose a reason for hiding this comment

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

Yay, thanks!

Comment on lines +379 to 391
switch n.String() {
case "net/url.URL":
return TypescriptType{ValueType: "string"}, nil
case "time.Time":
// We really should come up with a standard for time.
return TypescriptType{ValueType: "string"}, nil
case "database/sql.NullTime":
return TypescriptType{ValueType: "string", Optional: true}, nil
case "github.com/google/uuid.NullUUID":
return TypescriptType{ValueType: "string", Optional: true}, nil
case "github.com/google/uuid.UUID":
return TypescriptType{ValueType: "string"}, nil
}
Copy link
MemberAuthor

Choose a reason for hiding this comment

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

Would be dope if this was a yaml config instead.

@EmyrkEmyrkenabled auto-merge (squash)April 28, 2022 16:40
@EmyrkEmyrk merged commite330dc1 intomainApr 28, 2022
@EmyrkEmyrk deleted the stevenmasley/redo_typescript_gen branchApril 28, 2022 16:59
@missknissmisskniss added this to theV2 Beta milestoneMay 15, 2022
kylecarbs pushed a commit that referenced this pull requestJun 10, 2022
* feat: Switch packages for typescript generation codeSupports a larger set of types  - [x] Basics (string/int/etc)   - [x] Maps  - [x] Slices  - [x] Enums  - [x] Pointers
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@greyscaledgreyscaledgreyscaled approved these changes

@presleyppresleyppresleyp approved these changes

@f0sself0sself0ssel approved these changes

@kylecarbskylecarbsAwaiting requested review from kylecarbs

@BrunoQuaresmaBrunoQuaresmaAwaiting requested review from BrunoQuaresma

Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
V2 Beta
Development

Successfully merging this pull request may close these issues.

5 participants
@Emyrk@f0ssel@presleyp@greyscaled@misskniss

[8]ページ先頭

©2009-2025 Movatter.jp