1515// Refer to LICENSE for more information.
1616
1717using Confluent . Kafka ;
18+ using System ;
1819
1920
2021namespace Confluent . SchemaRegistry
@@ -38,11 +39,45 @@ namespace Confluent.SchemaRegistry
3839 public enum ReferenceSubjectNameStrategy
3940 {
4041 /// <summary>
41- /// (default): Use the reference name as the subject name.
42+ /// ReferenceName (default): Use the reference name as the subject name.
4243 /// </summary>
43- ReferenceName
44+ ReferenceName ,
45+ /// <summary>
46+ /// Qualified: Given a reference name, replace slashes with dots, and remove the .proto suffix to obtain the subject name.
47+ /// For example, mypackage/myfile.proto becomes mypackage.myfile.
48+ /// </summary>
49+ Qualified ,
50+ /// <summary>
51+ /// Custom: Use a custom reference subject name strategy resolver to determine the subject name.
52+ /// </summary>
53+ Custom
54+ }
55+
56+ /// <summary>
57+ /// Custom Reference Subject Name Strategy Interface
58+ /// </summary>
59+ public interface ICustomReferenceSubjectNameStrategy
60+ {
61+ /// <summary>
62+ /// Gets the subject name.
63+ /// </summary>
64+ /// <param name="context"></param>
65+ /// <param name="referenceName"></param>
66+ /// <returns></returns>
67+ string GetSubjectName ( SerializationContext context , string referenceName ) ;
68+ }
69+
70+ /// <summary>
71+ /// Configuration property names specific to the schema registry client.
72+ /// </summary>
73+ public static partial class PropertyNames
74+ {
75+ /// <summary>
76+ /// The subject name strategy to use for registration / lookup of referenced schemas
77+ /// Possible values: <see cref="Confluent.SchemaRegistry.ReferenceSubjectNameStrategy" />
78+ /// </summary>
79+ public const string ReferenceSubjectNameStrategy = "protobuf.serializer.reference.subject.name.strategy" ;
4480 }
45-
4681
4782 /// <summary>
4883 /// Extension methods for the ReferenceSubjectNameStrategy type.
@@ -52,7 +87,57 @@ public static class ReferenceSubjectNameStrategyExtensions
5287 /// <summary>
5388 /// Provide a functional implementation corresponding to the enum value.
5489 /// </summary>
55- public static ReferenceSubjectNameStrategyDelegate ToDelegate ( this ReferenceSubjectNameStrategy strategy )
56- => ( context , referenceName ) => referenceName ;
90+ public static ReferenceSubjectNameStrategyDelegate ToDelegate ( this ReferenceSubjectNameStrategy strategy , ICustomReferenceSubjectNameStrategy customReferenceSubjectNameStrategy = null )
91+ {
92+ return ( context , referenceName ) =>
93+ {
94+ switch ( strategy )
95+ {
96+ case ReferenceSubjectNameStrategy . ReferenceName :
97+ {
98+ return GetReferenceNameSubjectName ( context , referenceName ) ;
99+ }
100+ case ReferenceSubjectNameStrategy . Qualified :
101+ {
102+ return GetQualifiedSubjectName ( context , referenceName ) ;
103+ }
104+ case ReferenceSubjectNameStrategy . Custom :
105+ {
106+ if ( customReferenceSubjectNameStrategy == null )
107+ {
108+ throw new ArgumentException ( $ "Custom strategy requires a custom { nameof ( ICustomReferenceSubjectNameStrategy ) } implementation to be specified.") ;
109+ }
110+
111+ return customReferenceSubjectNameStrategy . GetSubjectName ( context , referenceName ) ;
112+ }
113+ default :
114+ {
115+ throw new ArgumentException ( $ "Unknown ${ PropertyNames . ReferenceSubjectNameStrategy } value: { strategy } .") ;
116+ }
117+ }
118+ } ;
119+ }
120+
121+ /// <summary>
122+ /// Get Subject Name
123+ /// </summary>
124+ /// <param name="context"></param>
125+ /// <param name="referenceName"></param>
126+ /// <returns></returns>
127+ public static string GetQualifiedSubjectName ( SerializationContext context , string referenceName )
128+ {
129+ return referenceName . Replace ( ".proto" , string . Empty ) . Replace ( "/" , "." ) ;
130+ }
131+
132+ /// <summary>
133+ /// Get Subject Name
134+ /// </summary>
135+ /// <param name="context"></param>
136+ /// <param name="referenceName"></param>
137+ /// <returns></returns>
138+ public static string GetReferenceNameSubjectName ( SerializationContext context , string referenceName )
139+ {
140+ return referenceName ;
141+ }
57142 }
58143}
0 commit comments