KiCad PCB EDA Suite
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
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 The 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
24template <typename T>
25void to_optional( const json& j, const char* key, std::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
36void 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;
44
45 if( v.download_url )
46 j["download_url"] = *v.download_url;
47
48 if( v.download_sha256 )
49 j["download_sha256"] = *v.download_sha256;
50
51 if( v.download_size )
52 j["download_size"] = *v.download_size;
53
54 if( v.install_size )
55 j["install_size"] = *v.install_size;
56
57 if( v.runtime )
58 j["runtime"] = *v.runtime;
59
60 if( v.platforms.size() > 0 )
61 nlohmann::to_json( j["platforms"], v.platforms );
62
63 if( v.kicad_version_max )
64 j["kicad_version_max"] = *v.kicad_version_max;
65
66 if( v.keep_on_update.size() > 0 )
67 nlohmann::to_json( j["keep_on_update"], v.keep_on_update );
68}
69
70
71void from_json( const json& j, PACKAGE_VERSION& v )
72{
73 j.at( "version" ).get_to( v.version );
74 j.at( "status" ).get_to( v.status );
75 j.at( "kicad_version" ).get_to( v.kicad_version );
76
77 to_optional( j, "version_epoch", v.version_epoch );
78 to_optional( j, "download_url", v.download_url );
79 to_optional( j, "download_sha256", v.download_sha256 );
80 to_optional( j, "download_size", v.download_size );
81 to_optional( j, "install_size", v.install_size );
82 to_optional( j, "kicad_version_max", v.kicad_version_max );
83 to_optional( j, "runtime", v.runtime );
84
85 if( j.contains( "platforms" ) )
86 j.at( "platforms" ).get_to( v.platforms );
87
88 if( j.contains( "keep_on_update" ) )
89 j.at( "keep_on_update" ).get_to( v.keep_on_update );
90}
91
92
93void to_json( json& j, const PCM_PACKAGE& p )
94{
95 j = json{ { "name", p.name },
96 { "description", p.description },
97 { "description_full", p.description_full },
98 { "identifier", p.identifier },
99 { "type", p.type },
100 { "author", p.author },
101 { "license", p.license },
102 { "resources", p.resources },
103 { "versions", p.versions } };
104
105 if( p.maintainer )
106 j["maintainer"] = *p.maintainer;
107
108 if( p.tags.size() > 0 )
109 j["tags"] = p.tags;
110
111 if( p.keep_on_update.size() > 0 )
112 j["keep_on_update"] = p.keep_on_update;
113}
114
115
116void from_json( const json& j, PCM_PACKAGE& p )
117{
118 j.at( "name" ).get_to( p.name );
119 j.at( "description" ).get_to( p.description );
120 j.at( "description_full" ).get_to( p.description_full );
121 j.at( "identifier" ).get_to( p.identifier );
122 j.at( "type" ).get_to( p.type );
123 j.at( "author" ).get_to( p.author );
124 j.at( "license" ).get_to( p.license );
125 j.at( "resources" ).get_to( p.resources );
126 j.at( "versions" ).get_to( p.versions );
127
128 to_optional( j, "maintainer", p.maintainer );
129 to_optional( j, "category", p.category );
130
131 if( p.type == PT_PLUGIN && p.category && p.category.value() == PC_FAB )
132 p.type = PT_FAB;
133
134 if( j.contains( "tags" ) )
135 j.at( "tags" ).get_to( p.tags );
136
137 if( j.contains( "keep_on_update" ) )
138 j.at( "keep_on_update" ).get_to( p.keep_on_update );
139}
140
141
143{
144 j = json{ { "url", r.url }, { "update_timestamp", r.update_timestamp } };
145
146 if( r.sha256 )
147 j["sha256"] = *r.sha256;
148}
149
150
152{
153 j.at( "url" ).get_to( r.url );
154 j.at( "update_timestamp" ).get_to( r.update_timestamp );
155
156 to_optional( j, "sha256", r.sha256 );
157}
158
159
160void to_json( json& j, const PCM_REPOSITORY& r )
161{
162 j = json{ { "name", r.name }, { "packages", r.packages } };
163
164 if( r.resources )
165 j["resources"] = *r.resources;
166
167 if( r.manifests )
168 j["manifests"] = *r.manifests;
169
170 if( r.maintainer )
171 j["maintainer"] = *r.maintainer;
172}
173
174
175void from_json( const json& j, PCM_REPOSITORY& r )
176{
177 j.at( "name" ).get_to( r.name );
178 j.at( "packages" ).get_to( r.packages );
179
180 to_optional( j, "resources", r.resources );
181 to_optional( j, "manifests", r.manifests );
182 to_optional( j, "maintainer", r.maintainer );
183}
184
185
187{
188 j = json{ { "package", e.package },
189 { "current_version", e.current_version },
190 { "repository_id", e.repository_id },
191 { "repository_name", e.repository_name },
192 { "install_timestamp", e.install_timestamp },
193 { "pinned", e.pinned } };
194}
195
196
198{
199 j.at( "package" ).get_to( e.package );
200 j.at( "current_version" ).get_to( e.current_version );
201 j.at( "repository_id" ).get_to( e.repository_id );
202 j.at( "repository_name" ).get_to( e.repository_name );
203 j.at( "install_timestamp" ).get_to( e.install_timestamp );
204
205 e.pinned = false;
206 if( j.contains( "pinned" ) )
207 j.at( "pinned" ).get_to( e.pinned );
208}
nlohmann::json json
Definition: gerbview.cpp:50
void to_json(json &j, const PACKAGE_VERSION &v)
Definition: pcm_data.cpp:36
void to_optional(const json &j, const char *key, std::optional< T > &dest)
Definition: pcm_data.cpp:25
void from_json(const json &j, PACKAGE_VERSION &v)
Definition: pcm_data.cpp:71
@ PC_FAB
Definition: pcm_data.h:55
@ PT_PLUGIN
Definition: pcm_data.h:44
@ PT_FAB
Definition: pcm_data.h:45
< Package version metadata Package metadata
Definition: pcm_data.h:91
std::optional< uint64_t > download_size
Definition: pcm_data.h:96
std::optional< uint64_t > install_size
Definition: pcm_data.h:97
wxString version
Definition: pcm_data.h:92
PCM_PACKAGE_VERSION_STATUS status
Definition: pcm_data.h:98
std::optional< wxString > kicad_version_max
Definition: pcm_data.h:101
std::optional< wxString > download_url
Definition: pcm_data.h:94
std::optional< PCM_PACKAGE_RUNTIME > runtime
Definition: pcm_data.h:103
std::optional< wxString > download_sha256
Definition: pcm_data.h:95
std::optional< int > version_epoch
Definition: pcm_data.h:93
std::vector< std::string > platforms
Definition: pcm_data.h:99
wxString kicad_version
Definition: pcm_data.h:100
std::vector< std::string > keep_on_update
Definition: pcm_data.h:102
Definition: pcm_data.h:157
wxString repository_name
Definition: pcm_data.h:161
PCM_PACKAGE package
Definition: pcm_data.h:158
uint64_t install_timestamp
Definition: pcm_data.h:162
wxString repository_id
Definition: pcm_data.h:160
wxString current_version
Definition: pcm_data.h:159
bool pinned
Definition: pcm_data.h:163
Repository reference to a resource.
Definition: pcm_data.h:113
wxString description
Definition: pcm_data.h:115
wxString description_full
Definition: pcm_data.h:116
wxString identifier
Definition: pcm_data.h:117
wxString license
Definition: pcm_data.h:122
std::vector< std::string > tags
Definition: pcm_data.h:124
STRING_MAP resources
Definition: pcm_data.h:123
std::optional< PCM_CONTACT > maintainer
Definition: pcm_data.h:121
wxString name
Definition: pcm_data.h:114
std::vector< PACKAGE_VERSION > versions
Definition: pcm_data.h:126
std::optional< PCM_PLUGIN_CATEGORY > category
Definition: pcm_data.h:119
PCM_PACKAGE_TYPE type
Definition: pcm_data.h:118
std::vector< std::string > keep_on_update
Definition: pcm_data.h:125
PCM_CONTACT author
Definition: pcm_data.h:120
Package installation entry.
Definition: pcm_data.h:141
PCM_RESOURCE_REFERENCE packages
Definition: pcm_data.h:143
std::optional< PCM_CONTACT > maintainer
Definition: pcm_data.h:146
wxString name
Definition: pcm_data.h:142
std::optional< PCM_RESOURCE_REFERENCE > manifests
Definition: pcm_data.h:145
std::optional< PCM_RESOURCE_REFERENCE > resources
Definition: pcm_data.h:144
Repository metadata.
Definition: pcm_data.h:132
std::optional< wxString > sha256
Definition: pcm_data.h:134
uint64_t update_timestamp
Definition: pcm_data.h:135