예제 #1
0
func processFile(inFile *descriptor.FileDescriptorProto) (*plugin.CodeGeneratorResponse_File, error) {
	if inFile.GetSyntax() != "proto3" {
		return nil, fmt.Errorf("Only proto3 syntax is supported")
	}

	outFile := &plugin.CodeGeneratorResponse_File{}

	inFilePath := inFile.GetName()
	inFileDir, inFileName := filepath.Split(inFilePath)

	shortModuleName := firstUpper(strings.TrimSuffix(inFileName, ".proto"))

	fullModuleName := ""
	outFileName := ""
	for _, segment := range strings.Split(inFileDir, "/") {
		if segment == "" {
			continue
		}
		fullModuleName += firstUpper(segment) + "."
		outFileName += firstUpper(segment) + "/"
	}
	fullModuleName += shortModuleName
	outFileName += shortModuleName + ".elm"

	outFile.Name = proto.String(outFileName)

	b := &bytes.Buffer{}
	fg := NewFileGenerator(b)

	fg.GenerateModule(fullModuleName)
	fg.GenerateImports()
	fg.GenerateRuntime()

	var err error

	// Top-level enums.
	for _, inEnum := range inFile.GetEnumType() {
		err = fg.GenerateEnumDefinition("", inEnum)
		if err != nil {
			return nil, err
		}

		err = fg.GenerateEnumDecoder("", inEnum)
		if err != nil {
			return nil, err
		}

		err = fg.GenerateEnumEncoder("", inEnum)
		if err != nil {
			return nil, err
		}
	}

	// Top-level messages.
	for _, inMessage := range inFile.GetMessageType() {
		err = fg.GenerateEverything("", inMessage)
		if err != nil {
			return nil, err
		}
	}

	outFile.Content = proto.String(b.String())

	return outFile, nil
}