@@ -18,9 +18,10 @@ const (
1818
1919// CmdMetadataFlags are flags expected by CmdMetadata.
2020type CmdMetadataFlags struct {
21- Help bool
22- NoColor bool
23- Format string
21+ Help bool
22+ NoColor bool
23+ Format string
24+ DataTypes bool
2425}
2526
2627// Init initializes the common flags available to CmdMetadata with sensible
@@ -44,6 +45,11 @@ func (f *CmdMetadataFlags) Init() {
4445 "format" , "f" , "" ,
4546 _h ,
4647 )
48+ pflag .BoolVar (
49+ & f .DataTypes ,
50+ "data-types" , false ,
51+ "show data type sizes within the data section." ,
52+ )
4753}
4854
4955func CmdMetadata (f CmdMetadataFlags , args []string , printHelp func ()) error {
@@ -71,7 +77,8 @@ func CmdMetadata(f CmdMetadataFlags, args []string, printHelp func()) error {
7177 }
7278
7379 // open tree.
74- db , err := maxminddb .Open (args [0 ])
80+ mmdbFile := args [0 ]
81+ db , err := maxminddb .Open (mmdbFile )
7582 if err != nil {
7683 return fmt .Errorf ("couldn't open mmdb file: %w" , err )
7784 }
@@ -86,7 +93,7 @@ func CmdMetadata(f CmdMetadataFlags, args []string, printHelp func()) error {
8693 metadataSectionStartOffset := 0
8794
8895 // Offset of this separator is used to determine the metadata start section, data section end and data section size.
89- offset , err := findSectionSeparator (args [ 0 ] , MetadataStartMarker )
96+ offset , err := findSectionSeparator (mmdbFile , MetadataStartMarker )
9097 if err != nil {
9198 return fmt .Errorf ("couldn't process the mmdb file: %w" , err )
9299 }
@@ -96,32 +103,57 @@ func CmdMetadata(f CmdMetadataFlags, args []string, printHelp func()) error {
96103 }
97104 dataSectionEndOffset = int (offset )
98105 dataSectionSize = int (offset ) - treeSize - 16
106+ var typeSizes TypeSizes
107+ if f .DataTypes {
108+ typeSizes , err = traverseDataSection (mmdbFile , int64 (dataSectionStartOffset ), int64 (dataSectionEndOffset ))
109+ if err != nil {
110+ return fmt .Errorf ("couldn't process the mmdb file: %w" , err )
111+ }
112+ }
99113 metadataSectionStartOffset = int (offset ) + len (MetadataStartMarker )
100114
101115 if f .Format == "pretty" {
102116 fmtEntry := color .New (color .FgCyan )
103117 fmtVal := color .New (color .FgGreen )
104- printlineGen := func (entryLen string ) func (string , string ) {
105- return func (name string , val string ) {
118+ printlineGen := func (indentSpace , entryLen string ) func (string , string , string ) {
119+ return func (name string , val string , valSimplified string ) {
106120 fmt .Printf (
107- "- %v %v\n " ,
121+ "%v- %v %v %v\n " ,
122+ indentSpace ,
108123 fmtEntry .Sprintf ("%-" + entryLen + "s" , name ),
109124 fmtVal .Sprintf ("%v" , val ),
125+ fmtVal .Sprintf ("%v" , valSimplified ),
110126 )
111127 }
112128 }
113- printline := printlineGen ("13" )
114- printline ("Binary Format" , binaryFmt )
115- printline ("Database Type" , mdFromLib .DatabaseType )
116- printline ("IP Version" , strconv .Itoa (int (mdFromLib .IPVersion )))
117- printline ("Record Size" , strconv .Itoa (int (mdFromLib .RecordSize )))
118- printline ("Node Count" , strconv .Itoa (int (mdFromLib .NodeCount )))
119- printline ("Tree Size" , strconv .Itoa (treeSize ))
120- printline ("Data Section Size" , strconv .Itoa (dataSectionSize ))
121- printline ("Data Section Start Offset" , strconv .Itoa (dataSectionStartOffset ))
122- printline ("Data Section End Offset" , strconv .Itoa (dataSectionEndOffset ))
123- printline ("Metadata Section Start Offset" , strconv .Itoa (metadataSectionStartOffset ))
124- printline ("Description" , "" )
129+
130+ printline := printlineGen ("" , "13" )
131+ printline ("Binary Format" , binaryFmt , "" )
132+ printline ("Database Type" , mdFromLib .DatabaseType , "" )
133+ printline ("IP Version" , strconv .Itoa (int (mdFromLib .IPVersion )), "" )
134+ printline ("Record Size" , strconv .Itoa (int (mdFromLib .RecordSize )), simplifySize (int64 (mdFromLib .RecordSize )))
135+ printline ("Node Count" , strconv .Itoa (int (mdFromLib .NodeCount )), simplifySize (int64 (mdFromLib .NodeCount )))
136+ printline ("Tree Size" , strconv .Itoa (treeSize ), simplifySize (int64 (treeSize )))
137+ printline ("Data Section Size" , strconv .Itoa (dataSectionSize ), simplifySize (int64 (dataSectionSize )))
138+ if f .DataTypes {
139+ typeSizePrintline := printlineGen (" " , "13" )
140+ typeSizePrintline ("Pointer Size" , strconv .Itoa (int (typeSizes .PointerSize )), simplifySize (typeSizes .PointerSize ))
141+ typeSizePrintline ("UTF-8 String Size" , strconv .Itoa (int (typeSizes .Utf8StringSize )), simplifySize (typeSizes .Utf8StringSize ))
142+ typeSizePrintline ("Double Size" , strconv .Itoa (int (typeSizes .DoubleSize )), simplifySize (typeSizes .DoubleSize ))
143+ typeSizePrintline ("Bytes Size" , strconv .Itoa (int (typeSizes .BytesSize )), simplifySize (typeSizes .BytesSize ))
144+ typeSizePrintline ("Unsigned 16-bit Integer Size" , strconv .Itoa (int (typeSizes .Unsigned16bitIntSize )), simplifySize (typeSizes .Unsigned16bitIntSize ))
145+ typeSizePrintline ("Unsigned 32-bit Integer Size" , strconv .Itoa (int (typeSizes .Unsigned32bitIntSize )), simplifySize (typeSizes .Unsigned32bitIntSize ))
146+ typeSizePrintline ("Signed 32-bit Integer Size" , strconv .Itoa (int (typeSizes .Signed32bitIntSize )), simplifySize (typeSizes .Signed32bitIntSize ))
147+ typeSizePrintline ("Unsigned 64-bit Integer Size" , strconv .Itoa (int (typeSizes .Unsigned64bitIntSize )), simplifySize (typeSizes .Unsigned64bitIntSize ))
148+ typeSizePrintline ("Unsigned 128-bit Integer Size" , strconv .Itoa (int (typeSizes .Unsigned128bitIntSize )), simplifySize (typeSizes .Unsigned128bitIntSize ))
149+ typeSizePrintline ("Map Key-Value Pair Count" , strconv .Itoa (int (typeSizes .MapKeyValueCount )), simplifySize (typeSizes .MapKeyValueCount ))
150+ typeSizePrintline ("Array Length" , strconv .Itoa (int (typeSizes .ArrayLength )), simplifySize (typeSizes .ArrayLength ))
151+ typeSizePrintline ("Float Size" , strconv .Itoa (int (typeSizes .FloatSize )), simplifySize (typeSizes .FloatSize ))
152+ }
153+ printline ("Data Section Start Offset" , strconv .Itoa (dataSectionStartOffset ), simplifySize (int64 (dataSectionStartOffset )))
154+ printline ("Data Section End Offset" , strconv .Itoa (dataSectionEndOffset ), simplifySize (int64 (dataSectionEndOffset )))
155+ printline ("Metadata Section Start Offset" , strconv .Itoa (metadataSectionStartOffset ), simplifySize (int64 (metadataSectionStartOffset )))
156+ printline ("Description" , "" , "" )
125157 descKeys , descVals := sortedMapKeysAndVals (mdFromLib .Description )
126158 longestDescKeyLen := strconv .Itoa (len (longestStrInStringSlice (descKeys )))
127159 for i := 0 ; i < len (descKeys ); i ++ {
@@ -131,9 +163,13 @@ func CmdMetadata(f CmdMetadataFlags, args []string, printHelp func()) error {
131163 fmtVal .Sprintf ("%v" , descVals [i ]),
132164 )
133165 }
134- printline ("Languages" , strings .Join (mdFromLib .Languages , ", " ))
135- printline ("Build Epoch" , strconv .Itoa (int (mdFromLib .BuildEpoch )))
166+ printline ("Languages" , strings .Join (mdFromLib .Languages , ", " ), "" )
167+ printline ("Build Epoch" , strconv .Itoa (int (mdFromLib .BuildEpoch )), "" )
136168 } else { // json
169+ var typeSizesPtr * TypeSizes
170+ if f .DataTypes {
171+ typeSizesPtr = & typeSizes
172+ }
137173 md := struct {
138174 BinaryFormatVsn string `json:"binary_format"`
139175 DatabaseType string `json:"db_type"`
@@ -142,6 +178,7 @@ func CmdMetadata(f CmdMetadataFlags, args []string, printHelp func()) error {
142178 NodeCount uint `json:"node_count"`
143179 TreeSize uint `json:"tree_size"`
144180 DataSectionSize uint `json:"data_section_size"`
181+ TypeSize * TypeSizes `json:"data_type_sizes,omitempty"`
145182 DataSectionStartOffset uint `json:"data_section_start_offset"`
146183 DataSectionEndOffset uint `json:"data_section_end_offset"`
147184 MetadataStartOffset uint `json:"metadata_section_start_offset"`
@@ -156,6 +193,7 @@ func CmdMetadata(f CmdMetadataFlags, args []string, printHelp func()) error {
156193 mdFromLib .NodeCount ,
157194 uint (treeSize ),
158195 uint (dataSectionSize ),
196+ typeSizesPtr ,
159197 uint (dataSectionStartOffset ),
160198 uint (dataSectionEndOffset ),
161199 uint (metadataSectionStartOffset ),
0 commit comments