@@ -16,11 +16,12 @@ limitations under the License.
1616
1717import {
1818 Context ,
19+ Field ,
1920 Kind ,
2021 Named ,
2122 Writer ,
2223} from "../../deps/@apexlang/core/model/mod.ts" ;
23- import { expandType , fieldName } from "./helpers.ts" ;
24+ import { defaultValueForType , expandType , fieldName } from "./helpers.ts" ;
2425import { translateAlias } from "./alias_visitor.ts" ;
2526import { formatComment } from "../utils/mod.ts" ;
2627import { getImports , GoVisitor } from "./go_visitor.ts" ;
@@ -29,6 +30,7 @@ import {
2930 StringValue ,
3031 Value ,
3132} from "../../deps/@apexlang/core/ast/mod.ts" ;
33+ import { snakeCase } from "../utils/utilities.ts" ;
3234
3335interface Serialize {
3436 value : string ;
@@ -45,14 +47,20 @@ export class StructVisitor extends GoVisitor {
4547 public override visitTypeBefore ( context : Context ) : void {
4648 const { type } = context ;
4749 super . triggerTypeBefore ( context ) ;
48- this . write ( formatComment ( "// " , type . description ) ) ;
49- this . write ( `type ${ type . name } struct {\n` ) ;
50-
5150 let writeTypeInfo = context . config . writeTypeInfo as boolean ;
5251 if ( writeTypeInfo == undefined ) {
5352 writeTypeInfo = this . writeTypeInfo ;
5453 }
5554
55+ if ( writeTypeInfo ) {
56+ this . write (
57+ `const TYPE_${ snakeCase ( type . name ) . toUpperCase ( ) } = "${ type . name } "\n\n` ,
58+ ) ;
59+ }
60+
61+ this . write ( formatComment ( "// " , type . description ) ) ;
62+ this . write ( `type ${ type . name } struct {\n` ) ;
63+
5664 if ( writeTypeInfo ) {
5765 this . write ( ` ns\n` ) ;
5866 }
@@ -100,6 +108,7 @@ export class StructVisitor extends GoVisitor {
100108
101109 public override visitTypeAfter ( context : Context ) : void {
102110 const { type } = context ;
111+ const importer = getImports ( context ) ;
103112 const receiver = type . name . substring ( 0 , 1 ) . toLowerCase ( ) ;
104113 this . write ( `}\n\n` ) ;
105114
@@ -108,8 +117,39 @@ export class StructVisitor extends GoVisitor {
108117 writeTypeInfo = this . writeTypeInfo ;
109118 }
110119 if ( writeTypeInfo ) {
111- this . write ( `func (${ receiver } *${ type . name } ) Type() string {
112- return "${ type . name } "
120+ const idField = type . fields . find ( ( f : Field ) => f . name == "id" ) ;
121+ if ( idField ) {
122+ importer . type ( idField . type ) ;
123+ const packageName = context . config . otherPackage ;
124+ const idType = expandType (
125+ idField . type ! ,
126+ packageName ,
127+ true ,
128+ translateAlias ( context ) ,
129+ ) ;
130+ const idDefault = defaultValueForType (
131+ context ,
132+ idField . type ,
133+ packageName ,
134+ ) ;
135+ this . write ( `func (${ receiver } *${ type . name } ) GetID() ${ idType } {
136+ if ${ receiver } == nil {
137+ return ${ idDefault }
138+ }
139+
140+ return ${ receiver } .ID
141+ }
142+
143+ func (${ receiver } *${ type . name } ) SetID(id ${ idType } ) {
144+ if ${ receiver } == nil {
145+ return
146+ }
147+
148+ ${ receiver } .ID = id
149+ }\n\n` ) ;
150+ }
151+ this . write ( `func (${ receiver } *${ type . name } ) GetType() string {
152+ return TYPE_${ snakeCase ( type . name ) . toUpperCase ( ) }
113153 }\n\n` ) ;
114154 }
115155 super . triggerTypeAfter ( context ) ;
0 commit comments