func loadMappingConfig()

in wstl1/mapping_engine/transform/transform.go [361:398]


func loadMappingConfig(loc *httppb.Location, typ hapb.MappingType) (*mappb.MappingConfig, error) {
	var data []byte
	switch l := loc.Location.(type) {
	case *httppb.Location_GcsLocation:
		d, err := gcsutil.ReadFromGcs(context.Background(), l.GcsLocation)
		if err != nil {
			return nil, fmt.Errorf("failed to read mapping config from GCS, %v", err)
		}
		data = d
	case *httppb.Location_LocalPath:
		d, err := ioutil.ReadFile(l.LocalPath)
		if err != nil {
			return nil, fmt.Errorf("failed to read library file with error %v", err)
		}
		data = d
	case *httppb.Location_UrlPath:
		return nil, fmt.Errorf("loading mappings from remote path %s is unsupported", l.UrlPath)
	default:
		return nil, fmt.Errorf("location type %T is unsupported", l)
	}

	mpc := &mappb.MappingConfig{}
	switch typ {
	case hapb.MappingType_RAW_PROTO:
		if err := prototext.Unmarshal(data, mpc); err != nil {
			return nil, err
		}
	case hapb.MappingType_MAPPING_LANGUAGE:
		lmpc, err := transpiler.Transpile(string(data))
		if err != nil {
			return nil, err
		}
		mpc = lmpc
	default:
		return nil, fmt.Errorf("invalid mapping config type %v", typ)
	}
	return mpc, nil
}