Beispiel #1
0
func TestArgsToTypeset(t *testing.T) {

	args := "Person=man,woman Animal=dog,cat Place=london,paris"
	ts, err := parse.TypeSet(args)

	if assert.NoError(t, err) {
		if assert.Equal(t, 8, len(ts)) {

			assert.Equal(t, ts[0]["Person"], "man")
			assert.Equal(t, ts[0]["Animal"], "dog")
			assert.Equal(t, ts[0]["Place"], "london")

			assert.Equal(t, ts[1]["Person"], "man")
			assert.Equal(t, ts[1]["Animal"], "dog")
			assert.Equal(t, ts[1]["Place"], "paris")

			assert.Equal(t, ts[2]["Person"], "man")
			assert.Equal(t, ts[2]["Animal"], "cat")
			assert.Equal(t, ts[2]["Place"], "london")

			assert.Equal(t, ts[3]["Person"], "man")
			assert.Equal(t, ts[3]["Animal"], "cat")
			assert.Equal(t, ts[3]["Place"], "paris")

			assert.Equal(t, ts[4]["Person"], "woman")
			assert.Equal(t, ts[4]["Animal"], "dog")
			assert.Equal(t, ts[4]["Place"], "london")

			assert.Equal(t, ts[5]["Person"], "woman")
			assert.Equal(t, ts[5]["Animal"], "dog")
			assert.Equal(t, ts[5]["Place"], "paris")

			assert.Equal(t, ts[6]["Person"], "woman")
			assert.Equal(t, ts[6]["Animal"], "cat")
			assert.Equal(t, ts[6]["Place"], "london")

			assert.Equal(t, ts[7]["Person"], "woman")
			assert.Equal(t, ts[7]["Animal"], "cat")
			assert.Equal(t, ts[7]["Place"], "paris")

		}
	}

	ts, err = parse.TypeSet("Person=man Animal=dog Place=london")
	if assert.NoError(t, err) {
		assert.Equal(t, 1, len(ts))
	}
	ts, err = parse.TypeSet("Person=1,2,3,4,5 Animal=1,2,3,4,5 Place=1,2,3,4,5")
	if assert.NoError(t, err) {
		assert.Equal(t, 125, len(ts))
	}
	ts, err = parse.TypeSet("Person=1 Animal=1,2,3,4,5 Place=1,2")
	if assert.NoError(t, err) {
		assert.Equal(t, 10, len(ts))
	}

	ts, err = parse.TypeSet("Person=interface{} Animal=interface{} Place=interface{}")
	if assert.NoError(t, err) {
		assert.Equal(t, 1, len(ts))
		assert.Equal(t, ts[0]["Animal"], "interface{}")
		assert.Equal(t, ts[0]["Person"], "interface{}")
		assert.Equal(t, ts[0]["Place"], "interface{}")
	}

}
Beispiel #2
0
func main() {
	var (
		in      = flag.String("in", "", "file to parse instead of stdin")
		out     = flag.String("out", "", "file to save output to instead of stdout")
		pkgName = flag.String("pkg", "", "package name for generated files")
		prefix  = "https://github.com/metabition/gennylib/raw/master/"
	)
	flag.Parse()
	args := flag.Args()

	if len(args) < 2 {
		usage()
		os.Exit(exitcodeInvalidArgs)
	}

	if strings.ToLower(args[0]) != "gen" && strings.ToLower(args[0]) != "get" {
		usage()
		os.Exit(exitcodeInvalidArgs)
	}

	// parse the typesets
	var setsArg = args[1]
	if strings.ToLower(args[0]) == "get" {
		setsArg = args[2]
	}
	typeSets, err := parse.TypeSet(setsArg)
	if err != nil {
		fatal(exitcodeInvalidTypeSet, err)
	}

	var outWriter io.Writer
	if len(*out) > 0 {
		outFile, err := os.Create(*out)
		if err != nil {
			fatal(exitcodeDestFileFailed, err)
		}
		defer outFile.Close()
		outWriter = outFile
	} else {
		outWriter = os.Stdout
	}

	if strings.ToLower(args[0]) == "get" {
		if len(args) != 3 {
			fmt.Println("not enough arguments to get")
			usage()
			os.Exit(exitcodeInvalidArgs)
		}
		r, err := http.Get(prefix + args[1])
		if err != nil {
			fatal(exitcodeGetFailed, err)
		}
		b, err := ioutil.ReadAll(r.Body)
		if err != nil {
			fatal(exitcodeGetFailed, err)
		}
		r.Body.Close()
		br := bytes.NewReader(b)
		err = gen(*in, *pkgName, br, typeSets, outWriter)
	} else if len(*in) > 0 {
		var file *os.File
		file, err = os.Open(*in)
		if err != nil {
			fatal(exitcodeSourceFileInvalid, err)
		}
		defer file.Close()
		err = gen(*in, *pkgName, file, typeSets, outWriter)
	} else {
		var source []byte
		source, err = ioutil.ReadAll(os.Stdin)
		if err != nil {
			fatal(exitcodeStdinFailed, err)
		}
		reader := bytes.NewReader(source)
		err = gen("stdin", *pkgName, reader, typeSets, outWriter)
	}

	// do the work
	if err != nil {
		fatal(exitcodeGenFailed, err)
	}

}