// 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 *api.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) }