diff --git a/sam/_gen.php b/sam/_gen.php index db1260216..0e8bf795f 100755 --- a/sam/_gen.php +++ b/sam/_gen.php @@ -46,9 +46,17 @@ foreach (array("structs", "handlers", "interfaces", "request", "") as $type) { $tpl->assign("name", $name); $tpl->assign("self", strtolower(substr($name, 0, 1))); $tpl->assign("api", $api); - $tpl->assign("fields", $api['struct']); + $tpl->assign("structs", $api['struct']); + $imports = array(); + foreach ($api['struct'] as $struct) { + if (isset($struct['imports'])) + foreach ($struct['imports'] as $import) { + $imports[] = $import; + } + } + $tpl->assign("imports", $imports); $tpl->assign("calls", $api['apis']); - $contents = $tpl->get(); + $contents = str_replace("\n\n}", "\n}", $tpl->get()); $save = true; if ($type === "" && file_exists($filename)) { diff --git a/sam/templates/http_structs.tpl b/sam/templates/http_structs.tpl index cdf6c8a7f..ba778a1f2 100644 --- a/sam/templates/http_structs.tpl +++ b/sam/templates/http_structs.tpl @@ -1,33 +1,49 @@ package {package} +{if !empty($imports)} +import ( +{foreach ($imports as $import)} + "{import}" +{/foreach} +) +{/if} + +type ({foreach $structs as $struct} + {if strpos($name, "Literal") !== false} - {foreach $fields as $field} + {foreach $struct.fields as $field} {field}{newline} {/foreach} {else} // {api.title} - type {name} struct { -{foreach $fields as $field} + {struct.name} struct { +{foreach $struct.fields as $field} {field.name} {field.type}{if $field.tag} `{$field.tag}`{/if}{newline} {/foreach} changed []string } -func ({name}) new() *{name} { - return &{name}{} +{/if}{/foreach} +) + +/* Constructors */ +{foreach $structs as $struct} +func ({struct.name}) New() *{struct.name} { + return &{struct.name}{} } +{/foreach} -{/if} - -{foreach $fields as $field} -func ({self} *{name}) Get{field.name}() {field.type} { +/* Getters/setters */ +{foreach $structs as $struct} +{foreach $struct.fields as $field} +func ({self} *{struct.name}) Get{field.name}() {field.type} { return {self}.{field.name} } -func ({self} *{name}) Set{field.name}(value {field.type}) *{name} {{if !$field.complex} +func ({self} *{struct.name}) Set{field.name}(value {field.type}) *{struct.name} {{if !$field.complex} if {self}.{field.name} != value { - {self}.changed = append({self}.changed, "{field.name|strtolower}") + {self}.changed = append({self}.changed, "{field.name}") {self}.{field.name} = value } {else} @@ -36,3 +52,5 @@ func ({self} *{name}) Set{field.name}(value {field.type}) *{name} {{if !$field.c return {self} } {/foreach} + +{/foreach} \ No newline at end of file