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

docs: generate Java data models that overwrite hashCode method #580

Merged
merged 3 commits into from
Jan 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion docs/languages/Java.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ Check out this [example for a live demonstration](../../examples/java-generate-e


## Include hashCode function for the class
TODO
To overwrite the `hashCode` method, use the preset `JAVA_COMMON_PRESET` and provide the option `hashCode: true`.

Check out this [example for a live demonstration](../../examples/java-generate-hashcode).

## Change the collection type for arrays
TODO
Expand Down
1 change: 1 addition & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ This directory contains a series of self-contained examples that you can use as
- [include-custom-function](./include-custom-function) - A basic example where a custom function is included.
- [java-generate-equals](./java-generate-equals) - A basic example that shows how to generate models that overwrite the `equal` method
- [generate-csharp-models](./generate-csharp-models) - A basic example to generate C# data models
- [java-generate-hashcode](./java-generate-hashcode) - A basic example that shows how to generate models that overwrite the `hashCode` method
17 changes: 17 additions & 0 deletions examples/java-generate-hashcode/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Java Generate `HashCode`

A basic example on how to generate models that overwrite `hashCode` method

## How to run this example

Run this example using:

```sh
npm i && npm run start
```

If you are on Windows, use the `start:windows` script instead:

```sh
npm i && npm run start:windows
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Should be able to generate a model to overwrite the hashCode method and should log expected output to console 1`] = `
Array [
"public class Root {
private Object email;

public Object getEmail() { return this.email; }
public void setEmail(Object email) { this.email = email; }

@Override
public int hashCode() {
return Objects.hash((Object)email);
}
}",
]
`;
14 changes: 14 additions & 0 deletions examples/java-generate-hashcode/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const spy = jest.spyOn(global.console, 'log').mockImplementation(() => { return; });
import {generate} from './index';

describe('Should be able to generate a model to overwrite the hashCode method', () => {
afterAll(() => {
jest.restoreAllMocks();
});
test('and should log expected output to console', async () => {
await generate();
//Generate is called 2x, so even though we expect 1 model, we double it
expect(spy.mock.calls.length).toEqual(2);
expect(spy.mock.calls[1]).toMatchSnapshot();
});
});
34 changes: 34 additions & 0 deletions examples/java-generate-hashcode/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { JavaGenerator, JAVA_COMMON_PRESET } from '../../src';

const generator = new JavaGenerator({
presets: [
{
preset: JAVA_COMMON_PRESET,
options: {
equal: false,
hashCode: true,
classToString: false
}
}
]
});

const jsonSchemaDraft7 = {
$schema: 'http://json-schema.org/draft-07/schema#',
type: 'object',
additionalProperties: false,
properties: {
email: {
type: 'string',
format: 'email'
}
}
};

export async function generate() : Promise<void> {
const models = await generator.generate(jsonSchemaDraft7);
for (const model of models) {
console.log(model.result);
}
}
generate();
10 changes: 10 additions & 0 deletions examples/java-generate-hashcode/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions examples/java-generate-hashcode/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"config" : { "example_name" : "java-generate-hashcode" },
"scripts": {
"install": "cd ../.. && npm i",
"start": "../../node_modules/.bin/ts-node --cwd ../../ ./examples/$npm_package_config_example_name/index.ts",
"start:windows": "..\\..\\node_modules\\.bin\\ts-node --cwd ..\\..\\ .\\examples\\%npm_package_config_example_name%\\index.ts",
"test": "../../node_modules/.bin/jest --config=../../jest.config.js ./examples/$npm_package_config_example_name/index.spec.ts",
"test:windows": "..\\..\\node_modules\\.bin\\jest --config=..\\..\\jest.config.js examples/%npm_package_config_example_name%/index.spec.ts"
}
}