func NewEndpointSpec()

in codegen/gateway.go [287:445]


func NewEndpointSpec(
	yamlFile string,
	h *PackageHelper,
	midSpecs map[string]*MiddlewareSpec,
) (*EndpointSpec, error) {
	_, err := os.Stat(yamlFile)
	if err != nil {
		return nil, errors.Wrapf(err, "Could not find file %s: ", yamlFile)
	}

	bytes, err := ioutil.ReadFile(yamlFile)
	if err != nil {
		return nil, errors.Wrapf(
			err, "Could not read yaml file %s: ", yamlFile,
		)
	}

	endpointConfigObj := map[string]interface{}{}
	err = yaml.Unmarshal(bytes, &endpointConfigObj)
	if err != nil {
		return nil, errors.Wrapf(
			err, "Could not parse yaml file: %s", yamlFile,
		)
	}

	if err := ensureFields(endpointConfigObj, mandatoryEndpointFields, yamlFile); err != nil {
		return nil, err
	}

	endpointType := endpointConfigObj["endpointType"]
	if endpointType == "http" {
		if err := ensureFields(endpointConfigObj, mandatoryHTTPEndpointFields, yamlFile); err != nil {
			return nil, err
		}

	}
	if endpointType != "http" && endpointType != "tchannel" {
		return nil, errors.Errorf(
			"Cannot support unknown endpointType for endpoint: %s", yamlFile,
		)
	}

	thriftFile := filepath.Join(
		h.IdlPath(), h.GetModuleIdlSubDir(true), endpointConfigObj["thriftFile"].(string),
	)

	mspec, err := NewModuleSpec(thriftFile, endpointType == "http", true, h)
	if err != nil {
		return nil, errors.Wrapf(
			err, "Could not build module spec for thrift: %s", thriftFile,
		)
	}

	var workflowImportPath string
	var clientID string
	var clientMethod string
	var isClientlessEndpoint bool

	workflowType := endpointConfigObj["workflowType"].(string)
	if workflowType == "httpClient" || workflowType == "tchannelClient" {
		iclientID, ok := endpointConfigObj["clientId"]
		if !ok {
			return nil, errors.Errorf(
				"endpoint config %q must have clientName field", yamlFile,
			)
		}
		if iclientID != nil {
			clientID = iclientID.(string)
		}
		iclientMethod, ok := endpointConfigObj["clientMethod"]
		if !ok {
			return nil, errors.Errorf(
				"endpoint config %q must have clientMethod field", yamlFile,
			)
		}
		if iclientMethod != nil {
			clientMethod = iclientMethod.(string)
		}
	} else if workflowType == customWorkflow {
		iworkflowImportPath, ok := endpointConfigObj["workflowImportPath"]
		if !ok {
			return nil, errors.Errorf(
				"endpoint config %q must have workflowImportPath field",
				yamlFile,
			)
		}
		workflowImportPath = iworkflowImportPath.(string)
	} else if workflowType == clientlessWorkflow {
		isClientlessEndpoint = true
	} else {
		return nil, errors.Errorf(
			"Invalid workflowType %q for endpoint %q",
			workflowType, yamlFile,
		)
	}

	dirName, err := filepath.Rel(h.ConfigRoot(), filepath.Dir(yamlFile))
	if err != nil {
		return nil, errors.Errorf("Config file is out of config root: %s", yamlFile)
	}

	goFolderName := filepath.Join(h.CodeGenTargetPath(), dirName)

	goStructsFileName := filepath.Join(
		h.CodeGenTargetPath(),
		dirName,
		filepath.Base(dirName)+"_structs.go",
	)

	goPackageName := filepath.Join(h.GoGatewayPackageName(), dirName)

	thriftInfo := endpointConfigObj["thriftMethodName"].(string)
	parts := strings.Split(thriftInfo, "::")
	if len(parts) != 2 {
		return nil, errors.Errorf(
			"Cannot read thriftMethodName %q for endpoint yaml file: %s",
			thriftInfo, yamlFile,
		)
	}
	var config map[string]interface{}
	if _, ok := endpointConfigObj["config"]; !ok {
		config = make(map[string]interface{})
	} else {
		config = endpointConfigObj["config"].(map[string]interface{})
	}

	espec := &EndpointSpec{
		ModuleSpec:           mspec,
		YAMLFile:             yamlFile,
		GoStructsFileName:    goStructsFileName,
		GoFolderName:         goFolderName,
		GoPackageName:        goPackageName,
		EndpointType:         endpointConfigObj["endpointType"].(string),
		EndpointID:           endpointConfigObj["endpointId"].(string),
		HandleID:             endpointConfigObj["handleId"].(string),
		ThriftFile:           thriftFile,
		ThriftServiceName:    parts[0],
		ThriftMethodName:     parts[1],
		WorkflowType:         workflowType,
		WorkflowImportPath:   workflowImportPath,
		IsClientlessEndpoint: isClientlessEndpoint,
		ClientID:             clientID,
		ClientMethod:         clientMethod,
		DefaultHeaders:       h.defaultHeaders,
		Config:               config,
	}

	defaultMidSpecs, err := getOrderedDefaultMiddlewareSpecs(
		h.ConfigRoot(),
		h.DefaultMiddlewareSpecs(),
		endpointType.(string))
	if err != nil {
		return nil, errors.Wrap(
			err, "error getting ordered default middleware specs",
		)
	}

	return augmentEndpointSpec(espec, endpointConfigObj, midSpecs, defaultMidSpecs)
}