// NewMediaTypesWriter returns a contexts code writer. // Media types contain the data used to render response bodies. func NewMediaTypesWriter(filename string) (*MediaTypesWriter, error) { file, err := codegen.SourceFileFor(filename) if err != nil { return nil, err } return &MediaTypesWriter{SourceFile: file, Validator: codegen.NewValidator()}, nil }
// NewUserTypesWriter returns a contexts code writer. // User types contain custom data structured defined in the DSL with "Type". func NewUserTypesWriter(filename string) (*UserTypesWriter, error) { file, err := codegen.SourceFileFor(filename) if err != nil { return nil, err } return &UserTypesWriter{ SourceFile: file, Finalizer: codegen.NewFinalizer(), Validator: codegen.NewValidator(), }, nil }
}) Describe("ValidationChecker", func() { Context("given an attribute definition and validations", func() { var attType design.DataType var validation *dslengine.ValidationDefinition att := new(design.AttributeDefinition) target := "val" context := "context" var code string // generated code JustBeforeEach(func() { att.Type = attType att.Validation = validation code = codegen.NewValidator().Code(att, false, false, false, target, context, 1, false) }) Context("of enum", func() { BeforeEach(func() { attType = design.Integer validation = &dslengine.ValidationDefinition{ Values: []interface{}{1, 2, 3}, } }) It("produces the validation go code", func() { Ω(code).Should(Equal(enumValCode)) }) })
// Generate is the generator entry point called by the meta generator. func Generate() (files []string, err error) { var ( outDir, target, ver string notest bool ) set := flag.NewFlagSet("app", flag.PanicOnError) set.String("design", "", "") set.StringVar(&outDir, "out", "", "") set.StringVar(&target, "pkg", "app", "") set.StringVar(&ver, "version", "", "") set.BoolVar(¬est, "notest", false, "") set.Bool("force", false, "") set.Parse(os.Args[1:]) outDir = filepath.Join(outDir, target) if err := codegen.CheckVersion(ver); err != nil { return nil, err } target = codegen.Goify(target, false) g := &Generator{OutDir: outDir, Target: target, NoTest: notest, API: design.Design, validator: codegen.NewValidator()} return g.Generate() }