Exemplo n.º 1
0
func validateConfig(driverName string) error {
	if driverName == "postgres" {
		return vala.BeginValidation().Validate(
			vala.StringNotEmpty(viper.GetString("postgres.user"), "postgres.user"),
			vala.StringNotEmpty(viper.GetString("postgres.host"), "postgres.host"),
			vala.Not(vala.Equals(viper.GetInt("postgres.port"), 0, "postgres.port")),
			vala.StringNotEmpty(viper.GetString("postgres.dbname"), "postgres.dbname"),
			vala.StringNotEmpty(viper.GetString("postgres.sslmode"), "postgres.sslmode"),
		).Check()
	}

	if driverName == "mysql" {
		return vala.BeginValidation().Validate(
			vala.StringNotEmpty(viper.GetString("mysql.user"), "mysql.user"),
			vala.StringNotEmpty(viper.GetString("mysql.host"), "mysql.host"),
			vala.Not(vala.Equals(viper.GetInt("mysql.port"), 0, "mysql.port")),
			vala.StringNotEmpty(viper.GetString("mysql.dbname"), "mysql.dbname"),
			vala.StringNotEmpty(viper.GetString("mysql.sslmode"), "mysql.sslmode"),
		).Check()
	}

	return errors.New("not a valid driver name")
}
Exemplo n.º 2
0
Arquivo: main.go Projeto: bz2/wadl2go
func main() {

	showDebug := flag.Bool("debug", false, "Controls debug log messages")
	wadlFilePath := flag.String("wadl-file", "", "Specifies which file to parse")
	toFile := flag.String("to-file", "", "Specifies the destination file")
	// TODO(katco-): Set default value to derived value from to-file PWD.
	packageName := flag.String("package-name", "main", "Specifies the package the generated file will be under.")
	userBaseUrl := flag.String("base-url", "", "Specifies a replacement for the given base URL.")
	flag.Parse()

	var debugBuff io.Writer
	// TODO(katco-): Switch on flag
	if *showDebug {
		debugBuff = os.Stderr
	} else {
		debugBuff = ioutil.Discard
	}
	debug = log.New(debugBuff, "DEBUG: ", 0)

	// Make sure we have a well-formed method.
	if err := vala.BeginValidation().Validate(
		vala.StringNotEmpty(*wadlFilePath, "wadl-file"),
		vala.StringNotEmpty(*toFile, "to-file"),
		vala.StringNotEmpty(*packageName, "package-name"),
	).Check(); err != nil {
		flag.Usage()
		os.Exit(0)
	}

	contents, err := ioutil.ReadFile(*wadlFilePath)
	if err != nil {
		panic(err)
	}

	var rawDoc WadlEntryDoc
	if err := xml.Unmarshal(contents, &rawDoc); err != nil && err != io.EOF {
		log.Fatal(err)
	}

	if len(wadl.WalkErrors) > 0 {
		panic(wadl.WalkErrors)
	}

	structuredDoc := WadlDoc{Methods: make(map[string]*WadlMethod)}

	// Pull type information from the grammars.
	var grammarTypes []*WadlVariable
	if rawDoc.Grammars == nil {
		log.Print("WARNING: No grammars in doc")
	} else {
		for _, grammar := range rawDoc.Grammars.Includes {
			fileType := path.Ext(string(grammar.Href))
			switch fileType {
			default:
				log.Printf("WARNING: skipping unsupported grammar type: %v", fileType)
			case ".json":
				rawSchema, err := readJsonSchemaFile(path.Join(path.Dir(*wadlFilePath), string(grammar.Href)))
				if err != nil {
					log.Fatalf("could not read JSON schema: %v", err)
				}
				grammarTypes = append(grammarTypes, rawJsonSchemaParamToParam(rawSchema)...)
			}
		}
	}

	// Build methods.
	for _, rawMethod := range rawDoc.Methods {
		debug.Printf("rawMethod: %s", rawMethod.Id)
		method := &WadlMethod{
			Documentation: rawDocsToDoc(rawMethod.Docs),
			Name:          string(rawMethod.Id),
			Type:          string(rawMethod.Name),
		}
		if rawMethod.Request != nil {
			debug.Println("request found")
			method.Arguments = append(method.Arguments, rawParamToVariable(rawMethod.Request.Params)...)
			for _, rawRep := range rawMethod.Request.Representations {
				// HACK(katco-): Care about more than JSON representations
				if string(rawRep.MediaType) != "application/json" {
					log.Printf("INFO: skipping request representation: %s", rawRep.MediaType)
					continue
				}

				// Check for parameters defined in the grammar.
				// HACK(katco-): We're specifically checking the json:ref attrbite for Openstack.
				debug.Printf("jsonref: %s", rawRep.JsonRef)
				if grammarRef := rawRep.JsonRef.String(); grammarRef != "" {
					// We know that any variables we might be trying
					// to reference will be at the top-level, and not
					// embedded.
					for _, grammarVar := range grammarTypes {
						if grammarVar.URI != grammarRef {
							continue
						}

						method.Arguments = append(method.Arguments, grammarVar)
					}
				}

				method.Arguments = append(method.Arguments, rawParamToVariable(rawRep.Params)...)
			}
		}
		for _, rawResponse := range rawMethod.Responses {
			method.Results = append(method.Results, rawParamToVariable(rawResponse.Params)...)
			method.AcceptableStatus = append(
				method.AcceptableStatus,
				strings.Split(string(rawResponse.Status), " ")...,
			)

			for _, rawRep := range rawResponse.Representations {
				// HACK(katco-): Care about more than JSON representations
				if string(rawRep.MediaType) != "application/json" {
					log.Printf("INFO: skipping response representation: %s", rawRep.MediaType)
					continue
				}

				// HACK(katco-): Don't assume <= 1 doc elements.
				example, err := dereferenceExampleFile(path.Dir(*wadlFilePath), rawRep.Docs[0].XsdGoPkgCDATA)
				if err != nil {
					log.Fatal(err)
				}

				debug.Printf("example: %s", example)

				method.ResultsExample = example
				method.Results = append(method.Results, rawParamToVariable(rawRep.Params)...)
				break
			}
		}
		structuredDoc.Methods[method.Name] = method
	}

	for _, resources := range rawDoc.Resourceses {
		baseUrl := *userBaseUrl
		if baseUrl == "" {
			baseUrl = string(resources.Base)
		}

		parsedBaseUrl, err := url.Parse(baseUrl)
		if err != nil {
			log.Fatalf("could not determine the base URL: %s", err)
		}
		debug.Println("base: " + parsedBaseUrl.String())
		recurseResources(structuredDoc.Methods, *parsedBaseUrl, nil, resources.Resources)
	}

	var methods []*WadlMethod
	for _, m := range structuredDoc.Methods {
		methods = append(methods, m)
	}

	var file bytes.Buffer
	Render(&file, *packageName, RenderMethodWithBulkTypes, methods...)

	ioutil.WriteFile(*toFile, file.Bytes(), 0640)
}