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

Add support for interface generation and enums #3047

Merged
merged 14 commits into from
Nov 25, 2023

Conversation

APshenkin
Copy link
Contributor

@APshenkin APshenkin commented Nov 10, 2023

Description

Some engineers prefer interfaces rather than classes for typescript types. So I added option in wails.json to choose between classes and interfaces. Classes are chosen as default for backward compatibility.

Also typescriptify supports providing types for enums. https://github.com/tkrajina/typescriptify-golang-structs#enums
As generation for them is sligtly different, I added EnumBind to App configuration like this

err := wails.Run(&options.App{
		Title:  "wails-app",
		Width:  1024,
		Height: 768,
		AssetServer: &assetserver.Options{
			Assets: assets,
		},
		BackgroundColour: &options.RGBA{R: 27, G: 38, B: 54, A: 1},
		Bind: []interface{}{
			app,
		},
		EnumBind: []interface{}{
			AllWeekdays,
		},
	})

This then generate proper enums in model.ts file, so types are not missing if you return something in structs

fixes #2258
And partially solve this #2104

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration using wails doctor.

  • Windows
  • macOS
  • Linux

Test Configuration

I created some showcase here https://github.com/APshenkin/wails-open-file/tree/feature/ts-interfaces-enums

Checklist:

  • I have updated website/src/pages/changelog.mdx with details of this PR
  • My code follows the general coding style of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

@APshenkin
Copy link
Contributor Author

@leaanthony curios about your input for that 😊

Copy link
Member

@leaanthony leaanthony left a comment

Choose a reason for hiding this comment

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

Thanks for this! Just a small question 👍

v2/internal/binding/binding.go Outdated Show resolved Hide resolved
Copy link
Member

@leaanthony leaanthony left a comment

Choose a reason for hiding this comment

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

Actually, we should have both enum + interface tests in binding/binding_test to make sure we're tracking this going forward.

@APshenkin
Copy link
Contributor Author

@leaanthony will write the tests for this on weekends

@APshenkin
Copy link
Contributor Author

@leaanthony added some tests

GeneratedJsEntityWithIntEnumTest,
GeneratedJsEntityWithStringEnumTest,
GeneratedJsEntityWithEnumTsName,
GeneratedJsEntityWithNestedStructInterfacesTest,
ImportedEnumTest,

As well as fixed the case, when struct contains imported enum (before it doesn't track type for it correctly)

@APshenkin
Copy link
Contributor Author

@leaanthony anything else that I maybe need to add it this PR?

Thanks

@leaanthony
Copy link
Member

No, just need time to review 🙏

@APshenkin
Copy link
Contributor Author

@leaanthony @stffabi
Just kind reminder on that 🙂

I want to use enums in my project as it will be much convenient to follow contracts between frontend and backend then 🙂

I currently use replace in go.mod but this adds additional overhead for CI/CD and other devs to install everything properly 🥲

@leaanthony
Copy link
Member

Thanks @APshenkin 🙏

@leaanthony leaanthony merged commit b9de31e into wailsapp:master Nov 25, 2023
3 of 4 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.

Typescript type definitions do not match Go definitions
2 participants