Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Yams.serialize converts string in ' to number #174

Open
sbarow opened this issue Feb 11, 2019 · 2 comments
Open

Yams.serialize converts string in ' to number #174

sbarow opened this issue Feb 11, 2019 · 2 comments

Comments

@sbarow
Copy link

sbarow commented Feb 11, 2019

Following on #116 when trying to serialise something likevalue: '10.0' the output will be value: 10.0.

Unfortunately doing value: !!str '10.0' still results in the output being number, tested with " and the same happens.

Example updating:

let updatedNode = try node.represented()
let serialized = try Yams.serialize(node: updatedNode)
try path.write(serialized)
@sbarow sbarow changed the title Yams.serialize converts string in '' to float Yams.serialize converts string in ' to number Feb 11, 2019
@sbarow
Copy link
Author

sbarow commented Feb 11, 2019

Doesn't look like this is specific to Yams but rather CYaml

Emitter.swift

    private func serializeScalar(_ scalar: Node.Scalar) throws {
	...
        let scalar_style = yaml_scalar_style_t(rawValue: scalar.style.rawValue) // Returns `any` as that's what's passed in.
	...
    }

Looking at this, a possible solution could be in Representer.swift with something like this:

private func canBecomeNumber(_ value: String) -> Bool {
    return Double(value) != nil
}

private func represent(_ value: Any) throws -> Node {
    if let string = value as? String {
        if value is String && canBecomeNumber(string) {
            return Node(string, .implicit, .singleQuoted)
        }
        return Node(string)
    } else if let representable = value as? NodeRepresentable {
        return try representable.represented()
    }
    throw YamlError.representer(problem: "Failed to represent \(value)")
}

If you're okay with this I can put up a PR.

@norio-nomura
Copy link
Collaborator

Thank you for filing an issue!

Could you provide test code to reproduce the issue?
The code I tried is as follows:

    func testSrializeNumberInSingleQuote() throws {
        let yaml = """
            value1: '10.0'
            value2: !!str '10.0'

            """
        let node = try Yams.compose(yaml: yaml)
        let updatedNode = try node.represented()
        let serialized = try Yams.serialize(node: updatedNode)
        print(serialized)
    }

result:

Test Suite 'Selected tests' started at 2019-02-16 09:23:46.575
Test Suite 'YamsTests.xctest' started at 2019-02-16 09:23:46.576
Test Suite 'EmitterTests' started at 2019-02-16 09:23:46.578
Test Case '-[YamsTests.EmitterTests testSrializeNumberInSingleQuote]' started.
value1: '10.0'
value2: '10.0'

Test Case '-[YamsTests.EmitterTests testSrializeNumberInSingleQuote]' passed (0.135 seconds).
Test Suite 'EmitterTests' passed at 2019-02-16 09:23:46.714.
	 Executed 1 test, with 0 failures (0 unexpected) in 0.135 (0.136) seconds
Test Suite 'YamsTests.xctest' passed at 2019-02-16 09:23:46.715.
	 Executed 1 test, with 0 failures (0 unexpected) in 0.135 (0.139) seconds
Test Suite 'Selected tests' passed at 2019-02-16 09:23:46.716.
	 Executed 1 test, with 0 failures (0 unexpected) in 0.135 (0.140) seconds
Program ended with exit code: 0```

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants