aluttik / go-crossplane

An unofficial Go port of the NGINX config/JSON converter crossplane
Apache License 2.0
50 stars 15 forks source link

how to use the Comment #7

Closed lyingcrow closed 3 years ago

lyingcrow commented 4 years ago

how to use the Comment? The comment cannot be made to take effect, Here are some examples,

func main(){
    file, err := os.Open("./build.json")
    if err != nil {
        panic(err)
    }

    content, err := ioutil.ReadAll(file)
    if err != nil {
        panic(err)
    }

    var payload crossplane.Payload
    if err := json.Unmarshal(content, &payload); err != nil {
        panic(err)
    }
    x:="# DDDD"
    s:=payload.Config[2].Parsed[0].Block
    *s = append(*s, crossplane.Directive{
        Directive: "location",
        Line:      14,
        Args:      []string{"xxxxx"},
        Block:     &[]crossplane.Directive{
            {
                Directive: "xxx",
                Line:      18,
                Args:      []string{"xxxxxx"},
                Includes:  nil,
                Block:     nil,
            },
        },
        Comment:   &x,
    })
    var buf bytes.Buffer
    if err := crossplane.Build(&buf, payload.Config[2], &crossplane.BuildOptions{}); err != nil {
        panic(err)
    }
    fmt.Println(buf.String())
}
aluttik commented 4 years ago

@hjinbi The problem here is that comments would need to be added by adding a new crossplane.Directive for the comment itself. For example:

    pstr := func(s string) *string { return &s }
    s := payload.Config[0].Parsed[0].Block
    *s = append(*s,
        crossplane.Directive{
            Directive: "#",
            Line:      13,
            Comment:   pstr(" above location"),
        },
        crossplane.Directive{
            Directive: "location",
            Line:      14,
            Args:      []string{"xxxxx"},
            Block: &[]crossplane.Directive{
                {
                    Directive: "#",
                    Line:      17,
                    Comment:   pstr(" above xxx"),
                },
                {
                    Directive: "xxx",
                    Line:      18,
                    Args:      []string{"xxxxxx"},
                },
                {
                    Directive: "#",
                    Line:      18,
                    Comment:   pstr(" beside xxx"),
                },
                {
                    Directive: "#",
                    Line:      19,
                    Comment:   pstr(" below xxx"),
                },
            },
        },
        crossplane.Directive{
            Directive: "#",
            Line:      15,
            Comment:   pstr(" below location"),
        },
    )

would add this nginx snippet to a config block:

    # above location
    location xxxxx {
        # above xxx
        xxx xxxxxx; # beside xxx
        # below xxx
    }
    # below location
aluttik commented 3 years ago

Let me know if that helps 🙂

lyingcrow commented 3 years ago

thank you for your help !