@@ -648,6 +648,52 @@ function nsValueToPythonNamespace(ns, clusterCount) {
648
648
return ns ;
649
649
}
650
650
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
+
651
697
/*
652
698
* @brief
653
699
*
@@ -701,12 +747,14 @@ async function zapTypeToClusterObjectType(type, isDecodable, options) {
701
747
return 'uint' + s [ 1 ] + '_t' ;
702
748
}
703
749
704
- const enumObj = await zclQuery . selectEnumByName (
750
+ const clusterCount = await getClusterCountForType (
705
751
this . global . db ,
752
+ pkgId ,
706
753
type ,
707
- pkgId
754
+ 'Enum' ,
755
+ options
708
756
) ;
709
- const ns = nsValueToNamespace ( options . hash . ns , enumObj . enumClusterCount ) ;
757
+ const ns = nsValueToNamespace ( options . hash . ns , clusterCount ) ;
710
758
return ns + asUpperCamelCase . call ( this , type , options ) ;
711
759
}
712
760
@@ -717,57 +765,28 @@ async function zapTypeToClusterObjectType(type, isDecodable, options) {
717
765
return 'uint' + s [ 1 ] + '_t' ;
718
766
}
719
767
720
- const bitmapObj = await zclQuery . selectBitmapByName (
768
+ const clusterCount = await getClusterCountForType (
721
769
this . global . db ,
722
770
pkgId ,
723
- type
724
- ) ;
725
- const ns = nsValueToNamespace (
726
- options . hash . ns ,
727
- bitmapObj . bitmapClusterCount
771
+ type ,
772
+ 'Bitmap' ,
773
+ options
728
774
) ;
775
+ const ns = nsValueToNamespace ( options . hash . ns , clusterCount ) ;
729
776
return (
730
777
'chip::BitMask<' + ns + asUpperCamelCase . call ( this , type , options ) + '>'
731
778
) ;
732
779
}
733
780
734
781
if ( types . isStruct ) {
735
782
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
+ ) ;
771
790
const ns = nsValueToNamespace ( options . hash . ns , clusterCount ) ;
772
791
return (
773
792
ns +
@@ -855,16 +874,14 @@ async function _zapTypeToPythonClusterObjectType(type, options) {
855
874
return 'uint' ;
856
875
}
857
876
858
- const enumObj = await zclQuery . selectEnumByName (
877
+ const clusterCount = await getClusterCountForType (
859
878
this . global . db ,
879
+ pkgId ,
860
880
type ,
861
- pkgId
862
- ) ;
863
-
864
- const ns = nsValueToPythonNamespace (
865
- options . hash . ns ,
866
- enumObj . enumClusterCount
881
+ 'Enum' ,
882
+ options
867
883
) ;
884
+ const ns = nsValueToPythonNamespace ( options . hash . ns , clusterCount ) ;
868
885
869
886
return ns + '.Enums.' + type ;
870
887
}
@@ -874,28 +891,14 @@ async function _zapTypeToPythonClusterObjectType(type, options) {
874
891
}
875
892
876
893
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
898
900
) ;
901
+ const ns = nsValueToPythonNamespace ( options . hash . ns , clusterCount ) ;
899
902
900
903
return ns + '.Structs.' + type ;
901
904
}
@@ -986,28 +989,14 @@ async function _getPythonFieldDefault(type, options) {
986
989
}
987
990
988
991
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
1010
998
) ;
999
+ const ns = nsValueToPythonNamespace ( options . hash . ns , clusterCount ) ;
1011
1000
1012
1001
return 'field(default_factory=lambda: ' + ns + '.Structs.' + type + '())' ;
1013
1002
}
0 commit comments