@@ -160,6 +160,14 @@ func gen(outputName string, obj types.Object, iface *types.Interface) error {
160160 }
161161 return false
162162 })
163+ fallbackParamName := util .GetUniqueName ("fallback" , func (nameExists string ) bool {
164+ for j := 0 ; j < iface .NumMethods (); j ++ {
165+ if iface .Method (j ).Name () == nameExists {
166+ return true
167+ }
168+ }
169+ return false
170+ })
163171
164172 // default interface generic types
165173 codeObjectTypes := util .AddTypeParamsList (objNamedType .TypeParams (), false )
@@ -197,6 +205,7 @@ func gen(outputName string, obj types.Object, iface *types.Interface) error {
197205 group .Id ("lock" ).Qual ("sync" , "Mutex" )
198206 }
199207 group .Id ("execCount" ).Map (String ()).Int ()
208+ group .Id (fallbackParamName ).Add (util .GetQualCode (obj .Type ()).TypesFunc (codeObjectTypes ))
200209
201210 // interface method impls
202211 for j := 0 ; j < iface .NumMethods (); j ++ {
@@ -292,6 +301,24 @@ func gen(outputName string, obj types.Object, iface *types.Interface) error {
292301 }
293302 }).Block (
294303 Do (func (s * Statement ) {
304+ s .If (Id ("d" ).Dot ("impl" + mtd .Name ()).Op ("==" ).Nil ().
305+ Op ("&&" ).
306+ Id ("d" ).Dot (fallbackParamName ).Op ("!=" ).Nil ()).BlockFunc (func (bgroup * Group ) {
307+ icall := Id ("d" ).Dot (fallbackParamName ).Dot (mtd .Name ()).CallFunc (func (igroup * Group ) {
308+ for k := 0 ; k < sig .Params ().Len (); k ++ {
309+ sigParam := sig .Params ().At (k )
310+ igroup .Id (util .ParamName (k , sigParam ))
311+ }
312+ })
313+ if sig .Results ().Len () == 0 {
314+ bgroup .Add (icall )
315+ bgroup .Return ()
316+ } else {
317+ bgroup .Add (Return (icall ))
318+ }
319+ })
320+ s .Line ()
321+
295322 call := Id ("d" ).Dot ("impl" + mtd .Name ()).CallFunc (func (cgroup * Group ) {
296323 cgroup .Id ("d" ).Dot ("createContext" ).Call (
297324 Lit (mtd .Name ()), Id ("d" ).Dot ("impl" + mtd .Name ()).Op ("==" ).Nil (),
@@ -407,6 +434,16 @@ func gen(outputName string, obj types.Object, iface *types.Interface) error {
407434 )
408435 }
409436
437+ // WithFallback option
438+ // # func WithQDTYPEFallback(fallback SOURCETYPE) QDTYPEOption {}
439+ f .Func ().Id ("With" + objNameExported + util .InitialToUpper (fallbackParamName )).TypesFunc (codeObjectTypesWithType ).Params (
440+ Id ("fallback" ).Add (util .GetQualCode (obj .Type ()).TypesFunc (codeObjectTypes )),
441+ ).Params (Id (objOption ).TypesFunc (codeObjectTypes )).Block (
442+ Return (Func ().Params (Id ("d" ).Op ("*" ).Id (objName ).TypesFunc (codeObjectTypes )).Block (
443+ Id ("d" ).Dot (fallbackParamName ).Op ("=" ).Id ("fallback" ),
444+ )),
445+ )
446+
410447 // method options
411448 for j := 0 ; j < iface .NumMethods (); j ++ {
412449 mtd := iface .Method (j )
0 commit comments