@@ -12,7 +12,67 @@ import {
1212} from "../common/index.ts" ;
1313import { getType } from "./index.ts" ;
1414
15- //TODO: use records
15+ export const generateDataRecord =
16+ ( schema : Definition , suffixNs : string [ ] = [ ] , intface ?: string ) : Generator =>
17+ ( name : string , pkg : string ) : string => {
18+ const properties = schema . properties || { } ;
19+ const props = defs ( properties ) ;
20+
21+ let code = `
22+ package ${ pkg } ;
23+
24+ public record ${ name } (` ;
25+ let i = 0 ;
26+ for ( const [ key , entry ] of props ) {
27+ i ++ ;
28+ /*if (isConst(entry)) {
29+ continue;
30+ }*/
31+ const comma = i <= Object . keys ( properties ) . length - 1 ? "," : "" ;
32+ code += `
33+ ${ getType ( entry , suffixNs ) } ${ key } ${ comma } ` ;
34+ }
35+
36+ code += `
37+ )${ intface ? ` implements ${ intface } ` : "" } {
38+
39+ public ${ name } (` ;
40+ let j = 0 ;
41+ for ( const [ key , entry ] of props ) {
42+ j ++ ;
43+ /*if (isConst(entry)) {
44+ continue;
45+ }*/
46+ const comma = j <= Object . keys ( properties ) . length - 1 ? "," : "" ;
47+ code += `
48+ ${ getType ( entry , suffixNs ) } ${ key } ${ comma } ` ;
49+ }
50+
51+ code += `
52+ ) {` ;
53+ for ( const [ key , entry ] of props ) {
54+ if ( isConst ( entry ) ) {
55+ code += `
56+ this.${ key } = ${ getValue ( entry ) } ;` ;
57+ continue ;
58+ }
59+ if ( Object . hasOwn ( entry , "default" ) ) {
60+ code += `
61+ this.${ key } = Object.requireNonNullElse(${ key } ,${ getDefault ( entry ) } );` ;
62+ } else {
63+ code += `
64+ this.${ key } = ${ key } ;` ;
65+ }
66+ }
67+
68+ code += `
69+ }
70+ }
71+ ` ;
72+
73+ return code ;
74+ } ;
75+
1676export const generateDataClass =
1777 ( schema : Definition , suffixNs : string [ ] = [ ] , intface ?: string ) : Generator =>
1878 ( name : string , pkg : string ) : string => {
@@ -23,15 +83,15 @@ export const generateDataClass =
2383package ${ pkg } ;
2484
2585public class ${ name } ${ intface ? ` implements ${ intface } ` : "" } {
26- ` ;
86+ ` ;
2787 for ( const [ key , entry ] of props ) {
2888 code += `
29- private final ${ getType ( entry , suffixNs ) } ${ key } ;`;
89+ private final ${ getType ( entry , suffixNs ) } ${ key } ;` ;
3090 }
3191
3292 code += `
3393
34- public ${ name } (`;
94+ public ${ name } (` ;
3595 let i = 0 ;
3696 for ( const [ key , entry ] of props ) {
3797 i ++ ;
@@ -40,41 +100,41 @@ public class ${name}${intface ? ` implements ${intface}` : ""} {
40100 }
41101 const comma = i <= Object . keys ( properties ) . length - 1 ? "," : "" ;
42102 code += `
43- ${ getType ( entry , suffixNs ) } ${ key } ${ comma } ` ;
103+ ${ getType ( entry , suffixNs ) } ${ key } ${ comma } ` ;
44104 }
45105
46106 code += `
47- ) {`;
107+ ) {` ;
48108 for ( const [ key , entry ] of props ) {
49109 if ( isConst ( entry ) ) {
50110 code += `
51- this.${ key } = ${ getValue ( entry ) } ;` ;
111+ this.${ key } = ${ getValue ( entry ) } ;` ;
52112 continue ;
53113 }
54114 if ( Object . hasOwn ( entry , "default" ) ) {
55115 code += `
56- this.${ key } = Object.requireNonNullElse(${ key } ,${ getDefault ( entry ) } );` ;
116+ this.${ key } = Object.requireNonNullElse(${ key } ,${ getDefault ( entry ) } );` ;
57117 } else {
58118 code += `
59- this.${ key } = ${ key } ;` ;
119+ this.${ key } = ${ key } ;` ;
60120 }
61121 }
62122
63123 code += `
64- }
65- ` ;
124+ }
125+ ` ;
66126 for ( const [ key , entry ] of props ) {
67127 code += `
68- public ${ getType ( entry , suffixNs ) } get${ firstLetterUpperCase ( key ) } () {
69- return ${ key } ;
70- }
71- ` ;
128+ public ${ getType ( entry , suffixNs ) } get${ firstLetterUpperCase ( key ) } () {
129+ return ${ key } ;
130+ }
131+ ` ;
72132 }
73133
74134 code += `
75135}
76136
77- ` ;
137+ ` ;
78138
79139 return code ;
80140 } ;
0 commit comments