@@ -648,6 +648,52 @@ function nsValueToPythonNamespace(ns, clusterCount) {
648648 return ns ;
649649}
650650
651+ // Not to be exported.
652+ //
653+ // dataType can be "Enum", "Struct", or "Bitmap".
654+ async function getClusterCountForType ( db , pkgId , type , dataType , options ) {
655+ if ( options . hash . cluster === '' ) {
656+ // This is a non-global data type that is associated with multiple
657+ // clusters: in that case our caller can pass in cluster="", and
658+ // we know that clusterCount > 1, so just set it to 2.
659+ return 2 ;
660+ }
661+
662+ const cluster = options . hash . cluster || options . hash . ns ;
663+ const typeObj = await zclQuery [ `select${ dataType } ByNameAndClusterName` ] (
664+ db ,
665+ type ,
666+ cluster ,
667+ pkgId
668+ ) ;
669+ if ( typeObj ) {
670+ return typeObj [ `${ dataType . toLowerCase ( ) } ClusterCount` ] ;
671+ }
672+
673+ if ( options . hash . cluster === undefined ) {
674+ // Backwards-compat case: we were called without ns or cluster at all
675+ // (not even cluster=""). Just get by name, since that's all we have to
676+ // work with. It won't work right when names are not unique, but that's
677+ // the best we can do.
678+ //
679+ // selectBitmapByName has different argument ordering from selectEnumByName
680+ // and selectStructByName, so account for that here.
681+ const isBitmap = dataType == 'Bitmap' ;
682+ const backwardsCompatTypeObj = await zclQuery [ `select${ dataType } ByName` ] (
683+ db ,
684+ isBitmap ? pkgId : type ,
685+ isBitmap ? type : pkgId
686+ ) ;
687+ return backwardsCompatTypeObj [ `${ dataType . toLowerCase ( ) } ClusterCount` ] ;
688+ }
689+
690+ // Something is wrong here. Possibly the caller is passing in a munged
691+ // cluster name. Just fail out instead of silently returning bad data.
692+ throw new Error (
693+ `Unable to find ${ dataType . toLowerCase ( ) } ${ type } in cluster ${ options . hash . cluster } `
694+ ) ;
695+ }
696+
651697/*
652698 * @brief
653699 *
@@ -701,12 +747,14 @@ async function zapTypeToClusterObjectType(type, isDecodable, options) {
701747 return 'uint' + s [ 1 ] + '_t' ;
702748 }
703749
704- const enumObj = await zclQuery . selectEnumByName (
750+ const clusterCount = await getClusterCountForType (
705751 this . global . db ,
752+ pkgId ,
706753 type ,
707- pkgId
754+ 'Enum' ,
755+ options
708756 ) ;
709- const ns = nsValueToNamespace ( options . hash . ns , enumObj . enumClusterCount ) ;
757+ const ns = nsValueToNamespace ( options . hash . ns , clusterCount ) ;
710758 return ns + asUpperCamelCase . call ( this , type , options ) ;
711759 }
712760
@@ -717,57 +765,28 @@ async function zapTypeToClusterObjectType(type, isDecodable, options) {
717765 return 'uint' + s [ 1 ] + '_t' ;
718766 }
719767
720- const bitmapObj = await zclQuery . selectBitmapByName (
768+ const clusterCount = await getClusterCountForType (
721769 this . global . db ,
722770 pkgId ,
723- type
724- ) ;
725- const ns = nsValueToNamespace (
726- options . hash . ns ,
727- bitmapObj . bitmapClusterCount
771+ type ,
772+ 'Bitmap' ,
773+ options
728774 ) ;
775+ const ns = nsValueToNamespace ( options . hash . ns , clusterCount ) ;
729776 return (
730777 'chip::BitMask<' + ns + asUpperCamelCase . call ( this , type , options ) + '>'
731778 ) ;
732779 }
733780
734781 if ( types . isStruct ) {
735782 passByReference = true ;
736- let clusterCount ;
737- if ( options . hash . cluster === '' ) {
738- // This is a non-global struct that is associated with multiple
739- // clusters: in that case our caller can pass in cluster="", and
740- // we know that clusterCount > 1, so just set it to 2.
741- clusterCount = 2 ;
742- } else {
743- const cluster = options . hash . cluster || options . hash . ns ;
744- const structObj = await zclQuery . selectStructByNameAndClusterName (
745- this . global . db ,
746- type ,
747- cluster ,
748- pkgId
749- ) ;
750- if ( structObj ) {
751- clusterCount = structObj . structClusterCount ;
752- } else if ( options . hash . cluster === undefined ) {
753- // Backwards-compat case: we were called without ns or cluster at all
754- // (not even cluster=""). Just get by name, since that's all we have to
755- // work with. It won't work right when names are not unique, but that's
756- // the best we can do.
757- const backwardsCompatStructObj = await zclQuery . selectStructByName (
758- this . global . db ,
759- type ,
760- pkgId
761- ) ;
762- clusterCount = backwardsCompatStructObj . structClusterCount ;
763- } else {
764- // Something is wrong here. Possibly the caller is passing in a munged
765- // cluster name. Just fail out instead of silently returning bad data.
766- throw new Error (
767- `Unable to find struct ${ type } in cluster ${ options . hash . cluster } `
768- ) ;
769- }
770- }
783+ const clusterCount = await getClusterCountForType (
784+ this . global . db ,
785+ pkgId ,
786+ type ,
787+ 'Struct' ,
788+ options
789+ ) ;
771790 const ns = nsValueToNamespace ( options . hash . ns , clusterCount ) ;
772791 return (
773792 ns +
@@ -855,16 +874,14 @@ async function _zapTypeToPythonClusterObjectType(type, options) {
855874 return 'uint' ;
856875 }
857876
858- const enumObj = await zclQuery . selectEnumByName (
877+ const clusterCount = await getClusterCountForType (
859878 this . global . db ,
879+ pkgId ,
860880 type ,
861- pkgId
862- ) ;
863-
864- const ns = nsValueToPythonNamespace (
865- options . hash . ns ,
866- enumObj . enumClusterCount
881+ 'Enum' ,
882+ options
867883 ) ;
884+ const ns = nsValueToPythonNamespace ( options . hash . ns , clusterCount ) ;
868885
869886 return ns + '.Enums.' + type ;
870887 }
@@ -874,28 +891,14 @@ async function _zapTypeToPythonClusterObjectType(type, options) {
874891 }
875892
876893 if ( await typeChecker ( 'isStruct' ) ) {
877- let structObj ;
878- if ( options . hash . cluster ) {
879- structObj = await zclQuery . selectStructByNameAndClusterName (
880- this . global . db ,
881- type ,
882- options . hash . cluster ,
883- pkgId
884- ) ;
885- } else {
886- // Backwards-compat case, which won't work when multiple structs have
887- // the same name.
888- structObj = await zclQuery . selectStructByName (
889- this . global . db ,
890- type ,
891- pkgId
892- ) ;
893- }
894-
895- const ns = nsValueToPythonNamespace (
896- options . hash . ns ,
897- structObj . structClusterCount
894+ const clusterCount = await getClusterCountForType (
895+ this . global . db ,
896+ pkgId ,
897+ type ,
898+ 'Struct' ,
899+ options
898900 ) ;
901+ const ns = nsValueToPythonNamespace ( options . hash . ns , clusterCount ) ;
899902
900903 return ns + '.Structs.' + type ;
901904 }
@@ -986,28 +989,14 @@ async function _getPythonFieldDefault(type, options) {
986989 }
987990
988991 if ( await typeChecker ( 'isStruct' ) ) {
989- let structObj ;
990- if ( options . hash . cluster ) {
991- structObj = await zclQuery . selectStructByNameAndClusterName (
992- this . global . db ,
993- type ,
994- options . hash . cluster ,
995- pkgId
996- ) ;
997- } else {
998- // Backwards-compat case, which won't work when multiple structs have
999- // the same name.
1000- structObj = await zclQuery . selectStructByName (
1001- this . global . db ,
1002- type ,
1003- pkgId
1004- ) ;
1005- }
1006-
1007- const ns = nsValueToPythonNamespace (
1008- options . hash . ns ,
1009- structObj . structClusterCount
992+ const clusterCount = await getClusterCountForType (
993+ this . global . db ,
994+ pkgId ,
995+ type ,
996+ 'Struct' ,
997+ options
1010998 ) ;
999+ const ns = nsValueToPythonNamespace ( options . hash . ns , clusterCount ) ;
10111000
10121001 return 'field(default_factory=lambda: ' + ns + '.Structs.' + type + '())' ;
10131002 }
0 commit comments