@@ -61,37 +61,62 @@ func GetOrCreateConverter(ctx api.StreamContext, format string, schemaId string,
6161 t = message .FormatJson
6262 }
6363 if cp , ok := modules .Converters [t ]; ok {
64- schemaType , hasSchema := modules .ConverterSchemas [t ]
65- if hasSchema {
66- schemaFileId := ""
67- if schemaId != "" {
68- r := strings .SplitN (schemaId , "." , 2 )
69- schemaFileId = r [0 ]
70- if len (r ) == 2 {
71- props ["$$messageName" ] = r [1 ]
72- }
73- }
74- ffs , err := schema .GetSchemaFile (schemaType , schemaFileId )
75- if err != nil {
76- return nil , err
77- }
78- return cp (ctx , ffs .SchemaFile , schemaFields , props )
79- } else {
80- return cp (ctx , schemaId , schemaFields , props )
64+ schemaPath , err := transSchemaId (t , schemaId , props )
65+ if err != nil {
66+ return nil , err
8167 }
68+ return cp (ctx , schemaPath , schemaFields , props )
8269 }
8370 return nil , fmt .Errorf ("format type %s not supported" , t )
8471}
8572
8673func GetConvertWriter (ctx api.StreamContext , format string , schemaId string , schema map [string ]* ast.JsonStreamField , props map [string ]any ) (message.ConvertWriter , error ) {
8774 t := strings .ToLower (format )
75+ schemaPath , err := transSchemaId (t , schemaId , map [string ]any {})
76+ if err != nil {
77+ return nil , err
78+ }
8879 if cw , ok := modules .ConvertWriters [t ]; ok {
89- return cw (ctx , schemaId , schema , props )
80+ return cw (ctx , schemaPath , schema , props )
9081 }
91- c , err := GetOrCreateConverter (ctx , t , schemaId , schema , props )
82+ c , err := GetOrCreateConverter (ctx , t , schemaPath , schema , props )
9283 if err != nil {
9384 return nil , err
9485 }
9586 ctx .GetLogger ().Infof ("writer %s not found, fall back to stack writer" , t )
9687 return NewStackWriter (ctx , c )
9788}
89+
90+ func GetMerger (ctx api.StreamContext , format string , schemaId string , schemaFields map [string ]* ast.JsonStreamField ) (modules.Merger , error ) {
91+ t := strings .ToLower (format )
92+ if mp , ok := modules .Mergers [t ]; ok {
93+ schemaPath , err := transSchemaId (t , schemaId , map [string ]any {})
94+ if err != nil {
95+ return nil , err
96+ }
97+ return mp (ctx , schemaPath , schemaFields )
98+ } else {
99+ return nil , fmt .Errorf ("merger %s not found" , t )
100+ }
101+ }
102+
103+ func transSchemaId (t , schemaId string , props map [string ]any ) (string , error ) {
104+ schemaType , requireSchema := modules .ConverterSchemas [t ]
105+ if requireSchema {
106+ schemaFileId := ""
107+ if schemaId != "" {
108+ r := strings .SplitN (schemaId , "." , 2 )
109+ schemaFileId = r [0 ]
110+ if len (r ) == 2 {
111+ props ["$$messageName" ] = r [1 ]
112+ }
113+ }
114+ ffs , err := schema .GetSchemaFile (schemaType , schemaFileId )
115+ if err != nil {
116+ return "" , err
117+ }
118+ return ffs .SchemaFile , nil
119+ } else { // If not require schema, just return the schemaId. And the register function need to deal with it by itself. Only the specific implementation defines the schemaId format.
120+ return schemaId , nil
121+ }
122+ }
0 commit comments