Skip to content

Commit 991b121

Browse files
authored
v.vmod: add encoder/1 too (#24034)
1 parent 6b3521f commit 991b121

3 files changed

Lines changed: 128 additions & 2 deletions

File tree

‎vlib/v/vmod/encoder.v‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
module vmod
2+
3+
import strings
4+
5+
fn quote(input string) string {
6+
if input.contains("'") {
7+
return '"' + input + '"'
8+
}
9+
return "'" + input + "'"
10+
}
11+
12+
fn encode_array(mut b strings.Builder, input []string) {
13+
if input.join('').len > 60 {
14+
b.writeln('[')
15+
for item in input {
16+
b.write_string('\t\t')
17+
b.write_string(quote(item))
18+
b.writeln(',')
19+
}
20+
b.writeln('\t]')
21+
} else {
22+
mut quoted := []string{}
23+
for item in input {
24+
quoted << quote(item)
25+
}
26+
b.write_string('[')
27+
b.write_string(quoted.join(', '))
28+
b.writeln(']')
29+
}
30+
}
31+
32+
pub fn encode(manifest Manifest) string {
33+
mut b := strings.new_builder(512)
34+
b.writeln('Module {')
35+
b.write_string('\tname: ')
36+
b.writeln(quote(manifest.name))
37+
b.write_string('\tdescription: ')
38+
b.writeln(quote(manifest.description))
39+
b.write_string('\tversion: ')
40+
b.writeln(quote(manifest.version))
41+
b.write_string('\tlicense: ')
42+
b.writeln(quote(manifest.license))
43+
if manifest.repo_url != '' {
44+
b.write_string('\trepo_url: ')
45+
b.writeln(quote(manifest.repo_url))
46+
}
47+
if manifest.author != '' {
48+
b.write_string('\author: ')
49+
b.writeln(quote(manifest.author))
50+
}
51+
b.write_string('\tdependencies: ')
52+
encode_array(mut b, manifest.dependencies)
53+
for key, values in manifest.unknown {
54+
b.write_string('\t')
55+
b.write_string(key)
56+
b.write_string(': ')
57+
encode_array(mut b, values)
58+
}
59+
b.write_string('}')
60+
return b.str()
61+
}

‎vlib/v/vmod/encoder_test.v‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import v.vmod
2+
3+
const mf_no_deps = "Module {
4+
name: 'V'
5+
description: 'The V programming language.'
6+
version: '0.4.10'
7+
license: 'MIT'
8+
repo_url: 'https://github.com/vlang/v'
9+
dependencies: []
10+
}"
11+
12+
fn test_encode_vmod_with_no_deps() {
13+
mf := vmod.decode(mf_no_deps)!
14+
assert vmod.encode(mf) == mf_no_deps
15+
}
16+
17+
const mf_with_deps_1 = "Module {
18+
name: 'V'
19+
description: 'The V programming language.'
20+
version: '0.4.10'
21+
license: 'MIT'
22+
repo_url: 'https://github.com/vlang/v'
23+
dependencies: ['hello', 'world']
24+
}"
25+
26+
const mf_with_deps_2 = 'Module {
27+
name: \'V\'
28+
description: "The V\' programming language."
29+
version: \'0.4.10\'
30+
license: \'MIT\'
31+
repo_url: \'https://github.com/vlang/v\'
32+
dependencies: [
33+
\'hello\',
34+
\'world\',
35+
\'fdsfgarhrhregwegewgeage\',
36+
\'geageaegeggegagaghjuktktytrrtrehitroorekfwepokooe\',
37+
]
38+
}'
39+
40+
fn test_encode_vmod_with_multiple_deps() {
41+
mf1 := vmod.decode(mf_with_deps_1)!
42+
assert vmod.encode(mf1) == mf_with_deps_1
43+
mf2 := vmod.decode(mf_with_deps_2)!
44+
assert vmod.encode(mf2) == mf_with_deps_2
45+
}
46+
47+
const mf_with_extra_fields = "Module {
48+
name: 'V'
49+
description: 'The V programming language.'
50+
version: '0.4.10'
51+
license: 'MIT'
52+
repo_url: 'https://github.com/vlang/v'
53+
dependencies: ['hello', 'world']
54+
extra_a: ['a', 'b', 'c']
55+
extra_b: [
56+
'aaaaaaaaaaaaaaaaaaaaa',
57+
'bbbbbbbbbbbbbbbbbbbbb',
58+
'ccccccccccccccccccccc',
59+
]
60+
}"
61+
62+
fn test_encode_vmod_with_extra_fields() {
63+
mf := vmod.decode(mf_with_extra_fields)!
64+
assert vmod.encode(mf) == mf_with_extra_fields
65+
}

‎vlib/v/vmod/parser.v‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ enum TokenKind {
2222
pub struct Manifest {
2323
pub mut:
2424
name string
25-
version string
2625
description string
27-
dependencies []string
26+
version string
2827
license string
2928
repo_url string
3029
author string
30+
dependencies []string
3131
unknown map[string][]string
3232
}
3333

0 commit comments

Comments
 (0)