Skip to content

Commit

Permalink
Generator for int64 and *int64
Browse files Browse the repository at this point in the history
  • Loading branch information
monde committed Mar 3, 2023
1 parent 3bb2eb5 commit bf0336a
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 8 deletions.
2 changes: 1 addition & 1 deletion openapi/generator/createdFiles.json

Large diffs are not rendered by default.

73 changes: 68 additions & 5 deletions openapi/generator/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function getType(obj, prefix = "") {
case 'dateTime' :
return String.raw`*time.Time`;
case 'integer' :
return String.raw`*int64`;
return String.raw`int64`;
case 'boolean' :
return String.raw`*bool`;
case 'hash' :
Expand Down Expand Up @@ -476,9 +476,8 @@ function getNewClientTagProps(operations) {
return tagResources.join("\n\t");
}

function buildModelProperties(model) {
function buildProperties(model) {
const properties = {};
const finalProps = [];

if (model.parent !== undefined) {
for (let parentProperty of model.parent.properties) {
Expand All @@ -497,14 +496,75 @@ function buildModelProperties(model) {
}
}

return properties;
}

function buildModelProperties(model) {
const finalProps = [];
const properties = buildProperties(model);

for (let propKey in properties) {
finalProps.push(structProp(properties[propKey].propertyName) + " " +
getType(properties[propKey], "*") + createJsonTag(properties[propKey].propertyName));
var type = getType(properties[propKey], "");
if (type === "int64") {
finalProps.push(structProp(properties[propKey].propertyName) + " " +
getType(properties[propKey], "*"));
finalProps.push(structProp(properties[propKey].propertyName) + "Ptr *" +
getType(properties[propKey], "*") + createJsonTag(properties[propKey].propertyName));
} else {
finalProps.push(structProp(properties[propKey].propertyName) + " " +
getType(properties[propKey], "*") + createJsonTag(properties[propKey].propertyName));
}
}

return finalProps.join("\n\t");
}

function hasInt64Ptrs(model) {
const properties = buildProperties(model);

for (let propKey in properties) {
var type = getType(properties[propKey], "");
if (type === "int64") {
return true;
}
}

return false;
}

function buildModelPropertiesForMarshal(model) {
const lines = [];
const properties = buildProperties(model);
for (let propKey in properties) {
var type = getType(properties[propKey], "");
if (type === "int64") {
var propertyName = structProp(properties[propKey].propertyName);
var propertyNamePtr = structProp(properties[propKey].propertyName + "Ptr");
lines.push("\tif a." + propertyName + " != 0 {");
lines.push("\t\tresult." + propertyNamePtr + " = Int64Ptr(a." + propertyName + ")");
lines.push("\t}");
}
}
return lines.join("\n\t");
}

function buildModelPropertiesForUnmarshal(model) {
const lines = [];
const properties = buildProperties(model);
for (let propKey in properties) {
var type = getType(properties[propKey], "");
if (type === "int64") {
var propertyName = structProp(properties[propKey].propertyName);
var propertyNamePtr = structProp(properties[propKey].propertyName + "Ptr");
lines.push("\tif result." + propertyNamePtr + " != nil {");
lines.push("\t\ta." + propertyName + " = *result." + propertyNamePtr);
lines.push("\t\ta." + propertyNamePtr + " = result." + propertyNamePtr);
lines.push("\t}");
}
}
return lines.join("\n\t");
}

function createJsonTag(propertyName) {
if (propertyName === "tokenLifetimeMinutes" ||
propertyName === "accessTokenLifetimeMinutes" ||
Expand Down Expand Up @@ -740,6 +800,9 @@ golang.process = ({spec, operations, models, handlebars}) => {
getClientTagResources,
getNewClientTagProps,
buildModelProperties,
hasInt64Ptrs,
buildModelPropertiesForMarshal,
buildModelPropertiesForUnmarshal,
responseModelInterface,
applicationModelInterface,
factorModelInterface,
Expand Down
32 changes: 31 additions & 1 deletion openapi/generator/templates/model.go.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ func New{{model.modelName}}() *{{model.modelName}} {
{{else if (eq prop.commonType "boolean") }}
{{structProp prop.propertyName}}: boolPtr({{prop.default}}),
{{else if (eq prop.commonType "integer") }}
{{structProp prop.propertyName}}: int64Ptr({{prop.default}}),
{{structProp prop.propertyName}}: {{prop.default}},
{{structProp prop.propertyName}}Ptr: Int64Ptr({{prop.default}}),
{{else}}
{{structProp prop.propertyName}}: {{prop.default}},
{{/if}}
Expand Down Expand Up @@ -135,6 +136,35 @@ func (a {{model.modelName}}) MarshalJSON() ([]byte, error) {
}

{{/if}}
{{#unless (eq model.modelName "GroupProfile")}}
{{#if (hasInt64Ptrs model) }}
func (a *{{model.modelName}}) MarshalJSON() ([]byte, error) {
type Alias {{model.modelName}}
type local struct {
*Alias
}
result := local{Alias: (*Alias)(a)}
{{{buildModelPropertiesForMarshal model}}}
return json.Marshal(&result)
}

func (a *{{model.modelName}}) UnmarshalJSON(data []byte) error {
type Alias {{model.modelName}}

result := &struct {
*Alias
}{
Alias: (*Alias)(a),
}
if err := json.Unmarshal(data, &result); err != nil {
return err
}
{{{buildModelPropertiesForUnmarshal model}}}
return nil
}

{{/if}}
{{/unless}}
{{#if (eq model.modelName "SocialAuthToken")}}
func (a *{{model.modelName}}) UnmarshalJSON(data []byte) error {
if string(data) == "null" || string(data) == `""` {
Expand Down
2 changes: 1 addition & 1 deletion openapi/generator/templates/okta.go.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,6 @@ func boolPtr(b bool) *bool {
return &b
}

func int64Ptr(i int64) *int64 {
func Int64Ptr(i int64) *int64 {
return &i
}

0 comments on commit bf0336a

Please sign in to comment.