From 04b67c5f3afcf50cac781edf63945c7d32c9fd4a Mon Sep 17 00:00:00 2001 From: Antoine Date: Tue, 24 Nov 2020 01:54:05 +0100 Subject: [PATCH] Create graph, node, link struct and parse hcl input file --- Makefile | 7 +++ bin/.gitkeep | 0 cmd/resolve.go | 40 +++++++++++++++++ go.mod | 7 +++ go.sum | 42 ++++++++++++++++++ graph.hcl | 108 ++++++++++++++++++++++++++++++++++++++++++++++ internal/graph.go | 12 ++++++ internal/link.go | 8 ++++ internal/node.go | 7 +++ 9 files changed, 231 insertions(+) create mode 100644 Makefile create mode 100644 bin/.gitkeep create mode 100644 cmd/resolve.go create mode 100644 go.mod create mode 100644 go.sum create mode 100644 graph.hcl create mode 100644 internal/graph.go create mode 100644 internal/link.go create mode 100644 internal/node.go diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..8043403 --- /dev/null +++ b/Makefile @@ -0,0 +1,7 @@ +.PHONY: build + +build: + go build -o bin/resolve cmd/resolve.go + +run: + ./bin/resolve -i graph.hcl diff --git a/bin/.gitkeep b/bin/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/cmd/resolve.go b/cmd/resolve.go new file mode 100644 index 0000000..203ac25 --- /dev/null +++ b/cmd/resolve.go @@ -0,0 +1,40 @@ +package main + +import ( + "flag" + "fmt" + "io/ioutil" + "log" + + "antoine-roux.ml/projects/go/dijkstra/internal" + "github.com/hashicorp/hcl/v2" + "github.com/hashicorp/hcl/v2/gohcl" + "github.com/hashicorp/hcl/v2/hclsyntax" +) + +func main() { + filename := flag.String("i", "-", "Input file use to generate graph") + flag.Parse() + + src, err := ioutil.ReadFile(*filename) + if err != nil { + log.Fatalln(err) + } + + file, diags := hclsyntax.ParseConfig(src, *filename, hcl.Pos{Line: 1, Column: 1}) + if diags.HasErrors() { + log.Fatalf("config parse: %s", fmt.Errorf("%w", diags)) + } + + graph := &internal.Graph{} + + diags = gohcl.DecodeBody(file.Body, nil, graph) + if diags.HasErrors() { + log.Fatalf("config parse: %s", fmt.Errorf("%w", diags)) + } + + fmt.Printf("%#v\n", *graph) + for _, p := range graph.Nodes { + fmt.Printf("%+v\n", p) + } +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..e65bdf5 --- /dev/null +++ b/go.mod @@ -0,0 +1,7 @@ +module antoine-roux.ml/projects/go/dijkstra + +go 1.15 + +require github.com/hashicorp/hcl/v2 v2.7.1 // indirect + +replace antoine-roux.ml/projects/go/dijkstra/internal => ./internal diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..d53ee18 --- /dev/null +++ b/go.sum @@ -0,0 +1,42 @@ +github.com/agext/levenshtein v1.2.1 h1:QmvMAjj2aEICytGiWzmxoE0x2KZvE0fvmqMOfy2tjT8= +github.com/agext/levenshtein v1.2.1/go.mod h1:JEDfjyjHDjOF/1e4FlBE/PkbqA9OfWu2ki2W0IB5558= +github.com/apparentlymart/go-dump v0.0.0-20180507223929-23540a00eaa3/go.mod h1:oL81AME2rN47vu18xqj1S1jPIPuN7afo62yKTNn3XMM= +github.com/apparentlymart/go-textseg v1.0.0 h1:rRmlIsPEEhUTIKQb7T++Nz/A5Q6C9IuX2wFoYVvnCs0= +github.com/apparentlymart/go-textseg v1.0.0/go.mod h1:z96Txxhf3xSFMPmb5X/1W05FF/Nj9VFpLOpjS5yuumk= +github.com/apparentlymart/go-textseg/v12 v12.0.0 h1:bNEQyAGak9tojivJNkoqWErVCQbjdL7GzRt3F8NvfJ0= +github.com/apparentlymart/go-textseg/v12 v12.0.0/go.mod h1:S/4uRK2UtaQttw1GenVJEynmyUenKwP++x/+DdGV/Ec= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/go-test/deep v1.0.3/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA= +github.com/golang/protobuf v1.1.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/google/go-cmp v0.3.1 h1:Xye71clBPdm5HgqGwUkwhbynsUJZhDbS20FvLhQ2izg= +github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= +github.com/hashicorp/hcl/v2 v2.7.1 h1:eZWfKEO93WyE4OnZSD5LVy70YHDV/mmQ49gpKDLpDVM= +github.com/hashicorp/hcl/v2 v2.7.1/go.mod h1:bQTN5mpo+jewjJgh8jr0JUguIi7qPHUF6yIfAEN3jqY= +github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/kylelemons/godebug v0.0.0-20170820004349-d65d576e9348/go.mod h1:B69LEHPfb2qLo0BaaOLcbitczOKLWTsrBG9LczfCD4k= +github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7 h1:DpOJ2HYzCv8LZP15IdmG+YdwD2luVPHITV96TkirNBM= +github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7/go.mod h1:ZXFpozHsX6DPmq2I0TCekCxypsnAUbP2oI0UX1GXzOo= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo= +github.com/spf13/pflag v1.0.2/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +github.com/vmihailenco/msgpack v3.3.3+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk= +github.com/zclconf/go-cty v1.2.0 h1:sPHsy7ADcIZQP3vILvTjrh74ZA175TFP5vqiNK1UmlI= +github.com/zclconf/go-cty v1.2.0/go.mod h1:hOPWgoHbaTUnI5k4D2ld+GRpFJSCe6bCM7m1q/N4PQ8= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20190426145343-a29dc8fdc734/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/net v0.0.0-20180811021610-c39426892332/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190502175342-a43fa875dd82/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs= +golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= diff --git a/graph.hcl b/graph.hcl new file mode 100644 index 0000000..7c4f829 --- /dev/null +++ b/graph.hcl @@ -0,0 +1,108 @@ +name = "graph1" + +nodes { + name = "A" + start = true +} + +nodes { + name = "B" +} + +nodes { + name = "C" +} + +nodes { + name = "D" +} + +nodes { + name = "E" +} + +nodes { + name = "F" +} + +nodes { + name = "G" +} + +nodes { + name = "H" +} + +nodes { + name = "I" +} + +nodes { + name = "J" +} + +links { + from = "A" + to = "B" + value = 85 +} + +links { + from = "A" + to = "C" + value = 217 +} + +links { + from = "A" + to = "E" + value = 173 +} + +links { + from = "B" + to = "F" + value = 80 +} + +links { + from = "C" + to = "G" + value = 186 +} + +links { + from = "C" + to = "H" + value = 103 +} + +links { + from = "D" + to = "H" + value = 183 +} + +links { + from = "F" + to = "I" + value = 250 +} + +links { + from = "H" + to = "J" + value = 167 +} + +links { + from = "I" + to = "J" + value = 84 +} + +links { + from = "E" + to = "J" + value = 502 +} diff --git a/internal/graph.go b/internal/graph.go new file mode 100644 index 0000000..82ea060 --- /dev/null +++ b/internal/graph.go @@ -0,0 +1,12 @@ +package internal + +import ( + "errors" +) + +type Graph struct { + Name string `hcl:"name"` + Nodes []*Node `hcl:"nodes,block"` + Links []*Link `hcl:"links,block"` +} + diff --git a/internal/link.go b/internal/link.go new file mode 100644 index 0000000..bee792c --- /dev/null +++ b/internal/link.go @@ -0,0 +1,8 @@ +package internal + +type Link struct { + From string `hcl:"from"` + To string `hcl:"to"` + Oriented bool `hcl:"oriented,optional"` + Value int `hcl:"value"` +} diff --git a/internal/node.go b/internal/node.go new file mode 100644 index 0000000..ff891c1 --- /dev/null +++ b/internal/node.go @@ -0,0 +1,7 @@ +package internal + +type Node struct { + Name string `hcl:"name"` + Value int `hcl:"value,optional"` + IsStart bool `hcl:"start,optional"` +}