Code Monkey home page Code Monkey logo

Comments (3)

flyx avatar flyx commented on July 24, 2024

See the docs on variant object types, specifically this:

The value of a discriminator field must be loaded before any value of a field that depends on it. Therefore, a YAML mapping cannot be used to serialize variant object types - the YAML specification explicitly states that the order of key-value pairs in a mapping must not be used to convey content information. So, any variant object type is serialized as a list of key-value pairs.

This should answer 1.

Concerning 2, multiple case constructs in an object are currently not supported. I am not entirely sure why, since it has been a long time since I wrote that code. I can't promise a timely fix but suggest you have a look at {.implicit.} and check if that can solve your problem. Your example would become:

import yaml/serialization, streams

type
    DataType = enum
        Default
        Sequence

    Inner1 {.implicit.} = object
        case kind: DataType
        of Default: value: string
        of Sequence: valueSeq: seq[string]

    Inner2 {.implicit.} = object
        case kind : DataType
        of Default: value: string
        of Sequence: valueSeq: seq[string]

    Data = object
        data: string
        inner1: Inner1
        inner2: Inner2

setTag(seq[string], "!list".Tag)

var node: Data

let s = newFileStream("in.yaml")
load(s, node)
s.close()

This can load the following YAML:

data: test
inner1: test # sets kind to Default and value to "test"
inner2: !list [test2, test3] # sets kind to Sequence.

from nimyaml.

zhenyamega avatar zhenyamega commented on July 24, 2024

I gave incorrect variable naming in the examples. I don't need variant object types. I need variant object fields and these variants may be several.

type
    DataType = enum
        Default
        Type1
        Type2

    Config = object
        name: string

        case kind: DataType
        of Type1:
            param1: string
            param2: string
        of Type2:
            param3: string
            param4: seq[string]
        else: discard
name: some_confing
kind: Type2
param3: str
param4:
  - str1
  - str2

from nimyaml.

flyx avatar flyx commented on July 24, 2024

That Config type is loadable but you must put the fields in a sequence for the reason quoted above from the docs. It's either that or use an implicit type and wrap the inner params into objects.

from nimyaml.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.