KiCad PCB EDA Suite
pcm_data.cpp
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2021 Andrew Lutsenko, anlutsenko at gmail dot com
5  * Copyright (C) 1992-2021 KiCad Developers, see AUTHORS.txt for contributors.
6  *
7  * This program is free software: you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by the
9  * Free Software Foundation, either version 3 of the License, or (at your
10  * option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #include "pcm_data.h"
22 
23 
24 template <typename T>
25 void to_optional( const json& j, const char* key, boost::optional<T>& dest )
26 {
27  if( j.contains( key ) )
28  {
29  T tmp;
30  j.at( key ).get_to( tmp );
31  dest.emplace( tmp );
32  }
33 }
34 
35 
36 void to_json( json& j, const PACKAGE_VERSION& v )
37 {
38  j = json{ { "version", v.version },
39  { "status", v.status },
40  { "kicad_version", v.kicad_version } };
41 
42  if( v.version_epoch )
43  j["version_epoch"] = v.version_epoch.get();
44 
45  if( v.download_url )
46  j["download_url"] = v.download_url.get();
47 
48  if( v.download_sha256 )
49  j["download_sha256"] = v.download_sha256.get();
50 
51  if( v.download_size )
52  j["download_size"] = v.download_size.get();
53 
54  if( v.install_size )
55  j["install_size"] = v.install_size.get();
56 
57  if( v.platforms.size() > 0 )
58  nlohmann::to_json( j["platforms"], v.platforms );
59 
60  if( v.kicad_version_max )
61  j["kicad_version_max"] = v.kicad_version_max.get();
62 }
63 
64 
65 void from_json( const json& j, PACKAGE_VERSION& v )
66 {
67  j.at( "version" ).get_to( v.version );
68  j.at( "status" ).get_to( v.status );
69  j.at( "kicad_version" ).get_to( v.kicad_version );
70 
71  to_optional( j, "version_epoch", v.version_epoch );
72  to_optional( j, "download_url", v.download_url );
73  to_optional( j, "download_sha256", v.download_sha256 );
74  to_optional( j, "download_size", v.download_size );
75  to_optional( j, "install_size", v.install_size );
76  to_optional( j, "kicad_version_max", v.kicad_version_max );
77 
78  if( j.contains( "platforms" ) )
79  j.at( "platforms" ).get_to( v.platforms );
80 }
81 
82 
83 void to_json( json& j, const PCM_PACKAGE& p )
84 {
85  j = json{ { "name", p.name },
86  { "description", p.description },
87  { "description_full", p.description_full },
88  { "identifier", p.identifier },
89  { "type", p.type },
90  { "author", p.author },
91  { "license", p.license },
92  { "resources", p.resources },
93  { "versions", p.versions } };
94 
95  if( p.maintainer )
96  j["maintainer"] = p.maintainer.get();
97 
98  if( p.tags.size() > 0 )
99  j["tags"] = p.tags;
100 }
101 
102 
103 void from_json( const json& j, PCM_PACKAGE& p )
104 {
105  j.at( "name" ).get_to( p.name );
106  j.at( "description" ).get_to( p.description );
107  j.at( "description_full" ).get_to( p.description_full );
108  j.at( "identifier" ).get_to( p.identifier );
109  j.at( "type" ).get_to( p.type );
110  j.at( "author" ).get_to( p.author );
111  j.at( "license" ).get_to( p.license );
112  j.at( "resources" ).get_to( p.resources );
113  j.at( "versions" ).get_to( p.versions );
114 
115  to_optional( j, "maintainer", p.maintainer );
116 
117  if( j.contains( "tags" ) )
118  j.at( "tags" ).get_to( p.tags );
119 }
120 
121 
123 {
124  j = json{ { "url", r.url }, { "update_timestamp", r.update_timestamp } };
125 
126  if( r.sha256 )
127  j["sha256"] = r.sha256.get();
128 }
129 
130 
132 {
133  j.at( "url" ).get_to( r.url );
134  j.at( "update_timestamp" ).get_to( r.update_timestamp );
135 
136  to_optional(j, "sha256", r.sha256 );
137 }
138 
139 
140 void to_json( json& j, const PCM_REPOSITORY& r )
141 {
142  j = json{ { "name", r.name }, { "packages", r.packages } };
143 
144  if( r.resources )
145  j["resources"] = r.resources.get();
146 
147  if( r.manifests )
148  j["manifests"] = r.manifests.get();
149 
150  if( r.maintainer )
151  j["maintainer"] = r.maintainer.get();
152 }
153 
154 
155 void from_json( const json& j, PCM_REPOSITORY& r )
156 {
157  j.at( "name" ).get_to( r.name );
158  j.at( "packages" ).get_to( r.packages );
159 
160  to_optional(j, "resources", r.resources );
161  to_optional(j, "manifests", r.manifests );
162  to_optional(j, "maintainer", r.maintainer );
163 }
< Package version metadata Package metadata
Definition: pcm_data.h:73
PCM_PACKAGE_VERSION_STATUS status
Definition: pcm_data.h:81
boost::optional< PCM_CONTACT > maintainer
Definition: pcm_data.h:101
PCM_PACKAGE_TYPE type
Definition: pcm_data.h:99
std::vector< std::string > platforms
Definition: pcm_data.h:82
boost::optional< uint64_t > download_size
Definition: pcm_data.h:79
boost::optional< uint64_t > install_size
Definition: pcm_data.h:80
void to_json(json &j, const PACKAGE_VERSION &v)
Definition: pcm_data.cpp:36
wxString description
Definition: pcm_data.h:96
void from_json(const json &j, PACKAGE_VERSION &v)
Definition: pcm_data.cpp:65
nlohmann::json json
Definition: gerbview.cpp:41
boost::optional< wxString > download_sha256
Definition: pcm_data.h:78
boost::optional< int > version_epoch
Definition: pcm_data.h:76
wxString description_full
Definition: pcm_data.h:97
boost::optional< wxString > download_url
Definition: pcm_data.h:77
Repository reference to a resource.
Definition: pcm_data.h:93
std::vector< std::string > tags
Definition: pcm_data.h:104
wxString name
Definition: pcm_data.h:95
E_SERIE r
Definition: eserie.cpp:41
STRING_MAP resources
Definition: pcm_data.h:103
std::vector< PACKAGE_VERSION > versions
Definition: pcm_data.h:105
void to_optional(const json &j, const char *key, boost::optional< T > &dest)
Definition: pcm_data.cpp:25
wxString identifier
Definition: pcm_data.h:98
wxString kicad_version
Definition: pcm_data.h:83
boost::optional< wxString > kicad_version_max
Definition: pcm_data.h:84
PCM_CONTACT author
Definition: pcm_data.h:100
wxString license
Definition: pcm_data.h:102
Package installation entry.
Definition: pcm_data.h:119
Repository metadata.
Definition: pcm_data.h:110
wxString version
Definition: pcm_data.h:75