Repository: cloudwego/kitex
File: tool/internal_pkg/pluginmode/thriftgo/hessian2.go (Go)

Code before:
		os.Exit(1)
	}

	// build idl ref config
	idlRefCfg := new(config.RawConfig)
	if len(data) == 0 {
		idlRefCfg.Ref = make(map[string]interface{})
	} else {
		err := yaml.Unmarshal(data, idlRefCfg)
		if err != nil {
			log.Warnf("Parse %s file failed: %s\n", file.Name(), err.Error())
			log.Warn("Please check whether the idl ref configuration is correct.")
			os.Exit(2)
		}
	}

	return idlRefCfg
}

var (
	JavaObjectRe                     = regexp.MustCompile(`\*java\.Object\b`)
	JavaExceptionRe                  = regexp.MustCompile(`\*java\.Exception\b`)
	JavaExceptionEmptyVerificationRe = regexp.MustCompile(`return p\.Exception != nil\b`)
)

// ReplaceObject args is the arguments from command, subDirPath used for xx/xx/xx
func ReplaceObject(args generator.Config, subDirPath string) error {
	output := args.OutputPath
	newPath := util.JoinPath(output, args.GenPath, subDirPath)
	fs, err := ioutil.ReadDir(newPath)
	if err != nil {
		return err
	}

	for _, f := range fs {
		fileName := util.JoinPath(args.OutputPath, args.GenPath, subDirPath, f.Name())
		if f.IsDir() {
			subDirPath := util.JoinPath(subDirPath, f.Name())
			if err = ReplaceObject(args, subDirPath); err != nil {
				return err
			}
		} else if strings.HasSuffix(f.Name(), ".go") {
			data, err := ioutil.ReadFile(fileName)
			if err != nil {
				return err
			}

			data = replaceJavaObject(data)
			data = replaceJavaException(data)
			data = replaceJavaExceptionEmptyVerification(data)
			if err = ioutil.WriteFile(fileName, data, 0o644); err != nil {

Review comment (suggestion):
`ReplaceObject` is not good enough now. Maybe `hessianPatchByReplace`.

First letter better be lowercase since it's not used outside this package. Also for the patterns above, no need to be exported symbols.

Code after:
		os.Exit(1)
	}

	// build idl ref config
	idlRefCfg := new(config.RawConfig)
	if len(data) == 0 {
		idlRefCfg.Ref = make(map[string]interface{})
	} else {
		err := yaml.Unmarshal(data, idlRefCfg)
		if err != nil {
			log.Warnf("Parse %s file failed: %s\n", file.Name(), err.Error())
			log.Warn("Please check whether the idl ref configuration is correct.")
			os.Exit(2)
		}
	}

	return idlRefCfg
}

var (
	javaObjectRe                     = regexp.MustCompile(`\*java\.Object\b`)
	javaExceptionRe                  = regexp.MustCompile(`\*java\.Exception\b`)
	javaExceptionEmptyVerificationRe = regexp.MustCompile(`return p\.Exception != nil\b`)
)

// Hessian2PatchByReplace args is the arguments from command, subDirPath used for xx/xx/xx
func Hessian2PatchByReplace(args generator.Config, subDirPath string) error {
	output := args.OutputPath
	newPath := util.JoinPath(output, args.GenPath, subDirPath)
	fs, err := ioutil.ReadDir(newPath)
	if err != nil {
		return err
	}

	for _, f := range fs {
		fileName := util.JoinPath(args.OutputPath, args.GenPath, subDirPath, f.Name())
		if f.IsDir() {
			subDirPath := util.JoinPath(subDirPath, f.Name())
			if err = Hessian2PatchByReplace(args, subDirPath); err != nil {
				return err
			}
		} else if strings.HasSuffix(f.Name(), ".go") {
			data, err := ioutil.ReadFile(fileName)
			if err != nil {
				return err
			}

			data = replaceJavaObject(data)
			data = replaceJavaException(data)
			data = replaceJavaExceptionEmptyVerification(data)
			if err = ioutil.WriteFile(fileName, data, 0o644); err != nil {