func genTypesOnlyAPI(file string) error { apiGen := &api.API{ NoRemoveUnusedShapes: true, NoRenameToplevelShapes: true, } apiGen.Attach(file) apiGen.Setup() // to reset imports so that timestamp has an entry in the map. apiGen.APIGoCode() var buf bytes.Buffer err := typesOnlyTplAPI.Execute(&buf, apiGen) if err != nil { panic(err) } code := strings.TrimSpace(buf.String()) code = util.GoFmt(code) // Ignore dir error, filepath will catch it for an invalid path. os.Mkdir(apiGen.PackageName(), 0755) // Fix imports. codeWithImports, err := imports.Process("", []byte(fmt.Sprintf("package %s\n\n%s", apiGen.PackageName(), code)), nil) if err != nil { fmt.Println(err) return err } outFile := filepath.Join(apiGen.PackageName(), "api.go") err = ioutil.WriteFile(outFile, []byte(fmt.Sprintf("%s\n%s", copyrightHeader, codeWithImports)), 0644) if err != nil { return err } return nil }
// GoCode returns the rendered Go code for the Shape. func (s *Shape) GoCode() string { code := s.Docstring() if !s.IsEnum() { code += "type " + s.ShapeName + " " } switch { case s.Type == "structure": code += "struct {\n" for _, n := range s.MemberNames() { m := s.MemberRefs[n] code += m.Docstring() if (m.Streaming || m.Shape.Streaming) && s.Payload == n { rtype := "io.ReadSeeker" if len(s.refs) > 1 { rtype = "aws.ReaderSeekCloser" } else if strings.HasSuffix(s.ShapeName, "Output") { rtype = "io.ReadCloser" } s.API.imports["io"] = true code += n + " " + rtype + " " + m.GoTags(false, s.IsRequired(n)) + "\n\n" } else { code += n + " " + m.GoType() + " " + m.GoTags(false, s.IsRequired(n)) + "\n\n" } } metaStruct := "metadata" + s.ShapeName ref := &ShapeRef{ShapeName: s.ShapeName, API: s.API, Shape: s} code += "\n" + metaStruct + " `json:\"-\" xml:\"-\"`\n" code += "}\n\n" code += "type " + metaStruct + " struct {\n" code += "SDKShapeTraits bool " + ref.GoTags(true, false) code += "}" if !s.API.NoStringerMethods { code += s.goCodeStringers() } case s.IsEnum(): code += "const (\n" for n, e := range s.Enum { code += fmt.Sprintf("\t// @enum %s\n\t%s = %q\n", s.ShapeName, s.EnumConsts[n], e) } code += ")" default: panic("Cannot generate toplevel shape for " + s.Type) } return util.GoFmt(code) }
func main() { out := generateTestSuite(os.Args[1]) if len(os.Args) == 3 { f, err := os.Create(os.Args[2]) defer f.Close() if err != nil { panic(err) } f.WriteString(util.GoFmt(out)) f.Close() c := exec.Command("gofmt", "-s", "-w", os.Args[2]) if err := c.Run(); err != nil { panic(err) } } else { fmt.Println(out) } }
// ParamsStructFromJSON returns a JSON string representation of a structure. func ParamsStructFromJSON(value interface{}, shape *Shape, prefixPackageName bool) string { f := paramFiller{prefixPackageName: prefixPackageName} return util.GoFmt(f.paramsStructAny(value, shape)) }
func writeGoFile(file string, layout string, args ...interface{}) error { return ioutil.WriteFile(file, []byte(util.GoFmt(fmt.Sprintf(layout, args...))), 0664) }